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 都能访问了。
感谢阅读,希望能帮助到大家,谢谢大家,对本站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无争山庄资源网 Copyright www.whwtcm.com
暂无“Nginx 多站点配置实例详解”评论...
更新日志
2025年10月31日
2025年10月31日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]