全网整合营销服务商

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

免费咨询热线: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 


相关文章: 关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)  全景视频制作网站有哪些,全景图怎么做成网页?  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  如何快速搭建支持数据库操作的智能建站平台?  网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?  如何在Windows服务器上快速搭建网站?  网站专业制作公司,网站编辑是做什么的?好做吗?工作前景如何?  在线制作视频的网站有哪些,电脑如何制作视频短片?  香港服务器网站生成指南:免费资源整合与高速稳定配置方案  定制建站哪家更专业可靠?推荐榜单揭晓  专业网站建设制作报价,网页设计制作要考什么证?  成都网站制作价格表,现在成都广电的单独网络宽带有多少的,资费是什么情况呢?  如何确认建站备案号应放置的具体位置?  南阳网站制作公司推荐,小学电子版试卷去哪里找资源好?  c# 服务器GC和工作站GC的区别和设置  成都响应式网站开发,dw怎么把手机适应页面变成网页?  国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?  手机网站制作与建设方案,手机网站如何建设?  如何用狗爹虚拟主机快速搭建网站?  股票网站制作软件,网上股票怎么开户?  如何在Golang中使用encoding/gob序列化对象_存储和传输数据  三星网站视频制作教程下载,三星w23网页如何全屏?  武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?  香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧  如何通过NAT技术实现内网高效建站?  学校建站服务器如何选型才能满足性能需求?  如何通过服务器快速搭建网站?完整步骤解析  已有域名如何快速搭建专属网站?  如何登录建站主机?访问步骤全解析  Swift中swift中的switch 语句  为什么Go需要go mod文件_Go go mod文件作用说明  如何用IIS7快速搭建并优化网站站点?  高端建站如何打造兼具美学与转化的品牌官网?  网站制作需要会哪些技术,建立一个网站要花费多少?  建站之星会员如何解锁更多建站功能?  建站ABC备案流程中有哪些关键注意事项?  可靠的网站设计制作软件,做网站设计需要什么样的电脑配置?  百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?  免费网站制作appp,免费制作app哪个平台好?  外贸公司网站制作,外贸网站建设一般有哪些步骤?  官网自助建站平台指南:在线制作、快速建站与模板选择全解析  PHP 500报错的快速解决方法  企业网站制作公司网页,推荐几家专业的天津网站制作公司?  建站主机数据库如何配置才能提升网站性能?  如何解决VPS建站LNMP环境配置常见问题?  岳西云建站教程与模板下载_一站式快速建站系统操作指南  内网网站制作软件,内网的网站如何发布到外网?  如何快速搭建个人网站并优化SEO?  无锡营销型网站制作公司,无锡网选车牌流程?  b2c电商网站制作流程,b2c水平综合的电商平台? 

您的项目需求

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