小知识:Nginx 多站点配置实例详解

Nginx 多站点配置实例详解

在一台 VPS 上,我们有时候需要同时跑几个 virtualenv。比如 virtualenv app1 跑的是 Django 的一个应用,而 virtualenv app2 跑的是 Tornado。那么如何配置 Nginx,让它同时支持这两个 virtualenv 的运行呢?

首先是 Nginx 的主配置,位于 etc/nginx/ngnix.conf,让它保持默认就行:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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:

?
1
service nginx restart

OK,两个虚拟环境的 app 都能访问了。

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

原文链接:http://www.nowamagic.net/academy/detail/1226277

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:nginx服务器配置解决ajax的跨域问题

2023-4-25 4:00:04

建站知识

小知识:linux下实现web数据同步的四种方式(性能比较)

2023-4-25 4:16:09

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索