Nginx 多站點(diǎn)配置實(shí)例詳解
Nginx 多站點(diǎn)配置實(shí)例詳解
在一臺 VPS 上,我們有時(shí)候需要同時(shí)跑幾個(gè) virtualenv。比如 virtualenv app1 跑的是 Django 的一個(gè)應(yīng)用,而 virtualenv app2 跑的是 Tornado。那么如何配置 Nginx,讓它同時(shí)支持這兩個(gè) virtualenv 的運(yùn)行呢?
首先是 Nginx 的主配置,位于 etc/nginx/ngnix.conf,讓它保持默認(rèn)就行:
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 文件夾下的所有配置文件。那么接下來的事情就簡單了,我們設(shè)計(jì)兩個(gè) .conf ,一個(gè)是 django 的配置,一個(gè)是 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,兩個(gè)虛擬環(huán)境的 app 都能訪問了。
感謝閱讀,希望能幫助到大家,謝謝大家,對本站的支持!
相關(guān)文章
Nginx下WordPress鏈接(url偽靜態(tài))301永久重定向?qū)崿F(xiàn)方法
在幾個(gè)blog程序中折騰的結(jié)果,導(dǎo)致url連續(xù)二次變化。這是第三次了。 nginx 通過rewrite 使用 permanent; 參數(shù) 成301永久url重定向2012-09-09
CentOS 7.0下nginx實(shí)現(xiàn)每天定時(shí)分割日志
大家都知道Nginx產(chǎn)生的日志都是存在一個(gè)文件,隨著網(wǎng)站運(yùn)行時(shí)間越長,日志文件的大小也在不斷增長,所以這個(gè)時(shí)候就需要實(shí)現(xiàn)定時(shí)分割,這篇文章主要介紹了在CentOS 7.0下nginx實(shí)現(xiàn)每天定時(shí)分割日志的相關(guān)資料,需要的朋友可以參考下。2017-04-04
nginx利用lua語言實(shí)現(xiàn)軟waf的示例代碼
這篇文章主要介紹了nginx利用lua語言實(shí)現(xiàn)軟waf,文中通過代碼示例和圖文結(jié)合的方式給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03
Nginx+Keepalived實(shí)現(xiàn)雙機(jī)主備的方法
這篇文章主要介紹了Nginx+Keepalived實(shí)現(xiàn)雙機(jī)主備的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
詳解nginx服務(wù)器http重定向到https的正確寫法
本篇文章主要介紹了nginx服務(wù)器http重定向到https的正確寫法 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
nginx配置同一域名同一端口下部署多個(gè)vue項(xiàng)目
本文主要介紹了nginx配置同一域名同一端口下部署多個(gè)vue項(xiàng)目,可以根據(jù)根路徑不同分別代理訪問不同項(xiàng)目,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06

