全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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


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


相关文章: 建站VPS推荐:2025年高性能服务器配置指南  常州企业网站制作公司,全国继续教育网怎么登录?  广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?  如何在西部数码注册域名并快速搭建网站?  简易网站制作视频教程,使用记事本编写一个简单的网页html文件?  怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?  已有域名如何免费搭建网站?  教学网站制作软件,学习*后期制作的网站有哪些?  ,有什么在线背英语单词效率比较高的网站?  如何通过云梦建站系统实现SEO快速优化?  百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?  h5网站制作工具有哪些,h5页面制作工具有哪些?  如何优化Golang Web性能_Golang HTTP服务器性能提升方法  如何选择PHP开源工具快速搭建网站?  建站主机服务器选型指南与性能优化方案解析  如何构建满足综合性能需求的优质建站方案?  制作充值网站的软件,做人力招聘为什么要自己交端口钱?  建站之星24小时客服电话如何获取?  小型网站建站如何选择虚拟主机?  详解jQuery中基本的动画方法  云南网站制作公司有哪些,云南最好的招聘网站是哪个?  建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析  制作表格网站有哪些,线上表格怎么弄?  如何选择靠谱的建站公司加盟品牌?  怀化网站制作公司,怀化新生儿上户网上办理流程?  制作证书网站有哪些,全国城建培训中心证书查询官网?  php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】  娃派WAP自助建站:免费模板+移动优化,快速打造专业网站  c# 服务器GC和工作站GC的区别和设置  焦点电影公司作品,电影焦点结局是什么?  专业网站制作企业网站,如何制作一个企业网站,建设网站的基本步骤有哪些?  招贴海报怎么做,什么是海报招贴?  如何在局域网内绑定自建网站域名?  如何选择美橙互联多站合一建站方案?  头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?  如何在七牛云存储上搭建网站并设置自定义域名?  建站之星CMS五站合一模板配置与SEO优化指南  建站ABC备案流程中有哪些关键注意事项?  定制建站方案优化指南:企业官网开发与建站费用解析  整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?  c# Task.Yield 的作用是什么 它和Task.Delay(1)有区别吗  php8.4新语法match怎么用_php8.4match表达式替代switch【方法】  宝塔建站助手安装配置与建站模板使用全流程解析  如何基于云服务器快速搭建网站及云盘系统?  深圳 网站制作,深圳招聘网站哪个比较好一点啊?  免费ppt制作网站,有没有值得推荐的免费PPT网站?  IOS倒计时设置UIButton标题title的抖动问题  html制作网站的步骤有哪些,iapp如何添加网页?  合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?  如何高效完成自助建站业务培训? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。