Nginx 多站点配置实例详解

发布时间 - 2026-01-11 00:21:34    点击率:

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

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


# Nginx多站点配置  # Nginx多站点配置实例  # Nginx如何实现多站点配置  # nginx 多站点配置方法集合  # 详解Nginx配置多站点需要踩的坑  # Nginx 的多站点配置方案  # Nginx+Tomcat多站点部署的方法  # docker Nginx PHP-FPM单机多站点布署的方法  # Nginx环境下WordPress的多站点功能配置详解  # Nginx多ip部署多站点的实现步骤  # 的是  # 让它  # 几个  # 这一  # 都能  # 一台  # 这两个  # 就行  # 希望能  # 注意到  # 谢谢大家  # 重启  # 它会  # 配置文件  # 加载  # remote_user  # main  # remote_addr  # types  # default_type 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  大连 网站制作,大连天途有线官网?  Laravel如何处理JSON字段的查询和更新_Laravel JSON列操作与查询技巧  Laravel如何生成URL和重定向?(路由助手函数)  Laravel如何集成第三方登录_Laravel Socialite实现微信QQ微博登录  如何在服务器上配置二级域名建站?  Laravel Asset编译怎么配置_Laravel Vite前端构建工具使用  如何用已有域名快速搭建网站?  WEB开发之注册页面验证码倒计时代码的实现  如何用腾讯建站主机快速创建免费网站?  java中使用zxing批量生成二维码立牌  高防服务器租用指南:配置选择与快速部署攻略  猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?  Laravel怎么实现验证码(Captcha)功能  如何快速选择适合个人网站的云服务器配置?  laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法  SQL查询语句优化的实用方法总结  如何在IIS中新建站点并配置端口与物理路径?  如何用PHP快速搭建CMS系统?  音响网站制作视频教程,隆霸音响官方网站?  如何彻底卸载建站之星软件?  Windows驱动无法加载错误解决方法_驱动签名验证失败处理步骤  Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置  rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted  Laravel如何处理文件上传_Laravel Storage门面实现文件存储与管理  如何在IIS7中新建站点?详细步骤解析  电商网站制作价格怎么算,网上拍卖流程以及规则?  PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)  Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)  西安专业网站制作公司有哪些,陕西省建行官方网站?  PHP正则匹配日期和时间(时间戳转换)的实例代码  Laravel中DTO是什么概念_在Laravel项目中使用数据传输对象(DTO)  如何在宝塔面板中创建新站点?  悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】  如何在阿里云香港服务器快速搭建网站?  如何在 Pandas 中基于一列条件计算另一列的分组均值  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  如何确认建站备案号应放置的具体位置?  历史网站制作软件,华为如何找回被删除的网站?  Laravel如何实现文件上传和存储?(本地与S3配置)  开心动漫网站制作软件下载,十分开心动画为何停播?  java获取注册ip实例  如何在Windows 2008云服务器安全搭建网站?  如何在阿里云购买域名并搭建网站?  JavaScript如何实现路由_前端路由原理是什么  微信h5制作网站有哪些,免费微信H5页面制作工具?  Laravel怎么定时执行任务_Laravel任务调度器Schedule配置与Cron设置【教程】  Laravel怎么实现软删除SoftDeletes_Laravel模型回收站功能与数据恢复【步骤】  高性能网站服务器部署指南:稳定运行与安全配置优化方案  Laravel如何从数据库删除数据_Laravel destroy和delete方法区别