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 都能访问了。

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

相关文章

  • LNMP 解决Access Denied错误详细介绍

    LNMP 解决Access Denied错误详细介绍

    这篇文章主要介绍了LNMP 解决Access Denied错误详细介绍的相关资料,需要的朋友可以参考下
    2016-10-10
  • Windows安装nginx1.10.1反向代理访问IIS网站

    Windows安装nginx1.10.1反向代理访问IIS网站

    这篇文章主要为大家详细介绍了Windows安装nginx1.10.1反向代理访问IIS网站的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Nginx学习之如何搭建文件防盗链服务的方法示例

    Nginx学习之如何搭建文件防盗链服务的方法示例

    这篇文章主要介绍了Nginx学习之如何搭建文件防盗链服务的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • Nginx中日志模块的应用和配置应用示例

    Nginx中日志模块的应用和配置应用示例

    Nginx是一款高性能的HTTP和反向代理服务器,广泛应用于互联网领域,这篇文章主要介绍了Nginx中日志模块的应用和配置,下面通过一个简单的实例来演示Nginx日志模块的应用和配置,需要的朋友可以参考下
    2024-02-02
  • nginx 反向代理之 proxy_pass的实现

    nginx 反向代理之 proxy_pass的实现

    这篇文章主要介绍了nginx 反向代理之 proxy_pass的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 图文详解nginx日志切割的实现

    图文详解nginx日志切割的实现

    这篇文章主要给大家介绍了关于nginx日志切割实现的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-01-01
  • Nginx如何配置根据路径转发详解

    Nginx如何配置根据路径转发详解

    Nginx是作为一个反向代理,转发,和负载均衡的服务器,也可以用于分布式,下面这篇文章主要给大家介绍了关于Nginx如何配置根据路径转发的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • 阿里云Linux系统Nginx配置多个域名的方法详解

    阿里云Linux系统Nginx配置多个域名的方法详解

    本篇文章主要介绍了阿里云Linux系统Nginx配置多个域名的方法详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • 详解NGINX如何统计网站的PV、UV、独立IP

    详解NGINX如何统计网站的PV、UV、独立IP

    做网站的都知道,平常经常要查询下网站PV、UV等网站的访问数据,这篇文章主要介绍了详解NGINX如何统计网站的PV、UV、独立IP ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • nginx强制使用https访问的方法(http跳转到https)

    nginx强制使用https访问的方法(http跳转到https)

    这篇文章主要介绍了nginx强制使用https访问的方法(http跳转到https),具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01

最新评论