Nginx 多站点配置实例详解
Nginx多站点配置实例详解
在一台VPS上,我们有时候需要同时跑几个virtualenv。比如virtualenvapp1跑的是Django的一个应用,而virtualenvapp2跑的是Tornado。那么如何配置Nginx,让它同时支持这两个virtualenv的运行呢?
首先是Nginx的主配置,位于etc/nginx/ngnix.conf,让它保持默认就行:
usernginx; worker_processes1; error_log/var/log/nginx/error.logwarn; pid/var/run/nginx.pid; events{ worker_connections1024; } http{ include/etc/nginx/mime.types; default_typeapplication/octet-stream; log_formatmain'$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.logmain; sendfileon; #tcp_nopushon; keepalive_timeout65; #gzipon; server{ listen80; server_name112.124.7.216; #server_namelocalhost; #if($host!='www.nowamagic.net'){ #rewrite^/(.*)$http://www.nowamagic.net/$1permanent; #} access_log/home/nowamagic/logs/access.log; error_log/home/nowamagic/logs/error.log; #root/root/nowamagic_venv/nowamagic_pj; location/{ uwsgi_pass127.0.0.1:8077; #includeuwsgi_params; include/etc/nginx/uwsgi_params; #uwsgi_pass127.0.0.1:8077; #uwsgi_paramUWSGI_SCRIPTindex; #uwsgi_paramUWSGI_PYHOME$document_root; #uwsgi_paramUWSGI_CHDIR$document_root; } location~\.php${ #roothtml; root/var/www/html; fastcgi_pass127.0.0.1:9000; fastcgi_indexindex.php; fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; includefastcgi_params; } access_logoff; } include/etc/nginx/conf.d/*.conf; }
注意到这一句,include/etc/nginx/conf.d/*.conf;它会加载conf.d文件夹下的所有配置文件。那么接下来的事情就简单了,我们设计两个.conf,一个是django的配置,一个是tornado的配置。
1.app1_django.conf
server{ listen80; server_name112.124.7.216; #server_namelocalhost; #if($host!='www.imofa.net'){ #rewrite^/(.*)$http://www.imofa.net/$1permanent; #} access_log/home/nowamagic/logs/access.log; error_log/home/nowamagic/logs/error.log; #root/root/nowamagic_venv/nowamagic_pj; location/{ uwsgi_pass127.0.0.1:8077; #includeuwsgi_params; include/etc/nginx/uwsgi_params; #uwsgi_pass127.0.0.1:8077; #uwsgi_paramUWSGI_SCRIPTindex; #uwsgi_paramUWSGI_PYHOME$document_root; #uwsgi_paramUWSGI_CHDIR$document_root; } location~\.php${ #roothtml; root/var/www/html; fastcgi_pass127.0.0.1:9000; fastcgi_indexindex.php; fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; includefastcgi_params; } access_logoff; }
下面是tornado的配置:
2.app2_tornado.conf
upstreamtornado{ server127.0.0.1:8888; } server{ listen80; root/root/nmapp2_venv; indexindex.pyindex.html; server_nameserver; location/{ #if(!-e$request_filename){ #rewrite^/(.*)$/index.py/$1last; #} } location~/index\.py{ proxy_pass_headerServer; proxy_set_headerHost$http_host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Scheme$scheme; proxy_passhttp://tornado; } }
重启Nginx:
servicenginxrestart
OK,两个虚拟环境的app都能访问了。
感谢阅读,希望能帮助到大家,谢谢大家,对本站的支持!