Nginx 多站点配置实例详解

 更新时间:2017年03月25日 10:09:47   投稿:lqh  
这篇文章主要介绍了Nginx 多站点配置实例详解的相关资料,需要的朋友可以参考下

Nginx 多站点配置实例详解

在一台 VPS 上,我们有时候需要同时跑几个 virtualenv。比如 virtualenv app1 跑的是 Django 的一个应用,而 virtualenv app2 跑的是 Tornado。那么如何配置 Nginx,让它同时支持这两个 virtualenv 的运行呢?

首先是 Nginx 的主配置,位于 etc/nginx/ngnix.conf,让它保持默认就行:

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid    /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  keepalive_timeout 65;

  #gzip on;

  server {
    listen    80;
    server_name 112.124.7.216;
    #server_name localhost;
    #if ($host != 'www.nowamagic.net' ) { 
    #  rewrite ^/(.*)$ http://www.nowamagic.net/$1 permanent; 
    #} 

    access_log /home/nowamagic/logs/access.log;
    error_log /home/nowamagic/logs/error.log;

    #root     /root/nowamagic_venv/nowamagic_pj;
    location / {
      uwsgi_pass 127.0.0.1:8077;
      #include uwsgi_params;
      include /etc/nginx/uwsgi_params;
      #uwsgi_pass 127.0.0.1:8077;
      #uwsgi_param UWSGI_SCRIPT index;
      #uwsgi_param UWSGI_PYHOME $document_root;
      #uwsgi_param UWSGI_CHDIR $document_root;
    }

    location ~ \.php$ { 
      #root     html; 
      root      /var/www/html;
      fastcgi_pass  127.0.0.1:9000; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      include    fastcgi_params; 
    }

    access_log off;
  }


  include /etc/nginx/conf.d/*.conf;
}

注意到这一句,include /etc/nginx/conf.d/*.conf; 它会加载 conf.d 文件夹下的所有配置文件。那么接下来的事情就简单了,我们设计两个 .conf ,一个是 django 的配置,一个是 tornado 的配置。

1. app1_django.conf

server {
  listen    80;
  server_name 112.124.7.216;
  #server_name localhost;
  #if ($host != 'www.imofa.net' ) { 
  #  rewrite ^/(.*)$ http://www.imofa.net/$1 permanent; 
  #} 

  access_log /home/nowamagic/logs/access.log;
  error_log /home/nowamagic/logs/error.log;

  #root     /root/nowamagic_venv/nowamagic_pj;
  location / {
    uwsgi_pass 127.0.0.1:8077;
    #include uwsgi_params;
    include /etc/nginx/uwsgi_params;
    #uwsgi_pass 127.0.0.1:8077;
    #uwsgi_param UWSGI_SCRIPT index;
    #uwsgi_param UWSGI_PYHOME $document_root;
    #uwsgi_param UWSGI_CHDIR $document_root;
  }

  location ~ \.php$ { 
    #root     html; 
    root      /var/www/html;
    fastcgi_pass  127.0.0.1:9000; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include    fastcgi_params; 
  }

  access_log off;
}

下面是 tornado 的配置:

2. app2_tornado.conf

upstream tornado {
  server 127.0.0.1:8888;
}
 
server {
  listen  80;
  root /root/nmapp2_venv;
  index index.py index.html;
 
  server_name server;
 
  location / {
    #if (!-e $request_filename) {
    #  rewrite ^/(.*)$ /index.py/$1 last;
    #}
  }
 
  location ~ /index\.py {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_pass http://tornado;
  }
}

重启 Nginx:

service nginx restart

OK,两个虚拟环境的 app 都能访问了。

感谢阅读,希望能帮助到大家,谢谢大家,对本站的支持!

相关文章

  • CentOS7下非编译安装Nginx的图文教程

    CentOS7下非编译安装Nginx的图文教程

    这篇文章主要为大家详细介绍了CentOS7下非编译安装Nginx的图文教程,文中通过示例图片进行了详细讲解,有需要的小伙伴可以跟随小编一起学习一下
    2023-10-10
  • Nginx禁止部分UserAgent访问的问题解决

    Nginx禁止部分UserAgent访问的问题解决

    本文主要介绍了使用Nginx禁止特定UserAgent访问域名,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-02-02
  • 如何自定义Nginx JSON日志格式配置

    如何自定义Nginx JSON日志格式配置

    Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种结构化日志格式特别适合ELK等日志分析系统处理,感兴趣的朋友一起看看吧
    2025-03-03
  • Fastdfs与nginx进行压缩图片比率

    Fastdfs与nginx进行压缩图片比率

    前阵子,工作搞了一下Fastdfs与nginx进行压缩图片比率存储在服务器中,今天应用下工作时间记录下.
    2014-07-07
  • Nginx如何安装配置Lua支持

    Nginx如何安装配置Lua支持

    这篇文章主要介绍了Nginx如何安装配置Lua支持方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • nginx之内存池的实现

    nginx之内存池的实现

    本文主要介绍了nginx之内存池的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • Linux centos7环境下Nginx安装教程

    Linux centos7环境下Nginx安装教程

    这篇文章主要为大家详细介绍了Linux centos7环境下Nginx安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Nginx location和proxy_pass配置示例详解

    Nginx location和proxy_pass配置示例详解

    这篇文章主要介绍了Nginx location和proxy_pass配置的相关资料,本文详细探讨了Nginx配置中`location`和`proxy_pass`指令的不同组合方式及其对请求转发路径的影响,通过列举多种组合,展示了`location`匹配目录与`proxy_pass`地址路径如何相互作用,需要的朋友可以参考下
    2024-11-11
  • Nginx的一些基本功能极速入门

    Nginx的一些基本功能极速入门

    本文主要介绍一些Nginx的最基本功能以及简单配置,但不包括Nginx的安装部署以及实现原理。有兴趣的可以了解一下。
    2016-12-12
  • Nginx服务器做负载均衡反向代理的超级攻略

    Nginx服务器做负载均衡反向代理的超级攻略

    这篇文章主要介绍了Nginx服务器做负载均衡反向代理的超级攻略,包括缓存的相关设定以及负载均衡的一些常见问题的解决,极力推荐!需要的朋友可以参考下
    2015-08-08

最新评论