Nginx 配置端口转发
本文介绍如何为
Nginx
配置端口转发。经常在CentOS
系统中搭建各种网站,为了满足各种不同的网站共用80/443
端口,我们可以采用Nginx
的端口转发功能,将80/443
端口通过域名映射到后端的多个端口。
# 配置端口转发
进入/etc/nginx/conf.d 目录下,默认文件如下:
[root@ady_cn ~]# cd /etc/nginx/conf.d
[root@ady_cn conf.d]# ll
total 12
-rw-r--r-- 1 root root 1290 Jun 17 05:34 default.conf
-rw-r--r-- 1 root root 466 Jun 17 05:34 ssl.conf
-rw-r--r-- 1 root root 283 Jun 17 05:34 virtual.conf
[root@ady_cn conf.d]#
1
2
3
4
5
6
7
2
3
4
5
6
7
在/etc/nginx/conf.d 目录下创建 proxy.conf 文件,文件内容如下:
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
在/etc/nginx/conf.d 中,创建 nginx.conf 文件,配置信息如下:
#user nobody;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 64;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 100;
# set request cache
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
# open gzip model
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
upstream www.qikemi.com {
server 127.0.0.1:8085;
}
upstream www.ady.cn {
server 127.0.0.1:8086;
}
upstream www.xiaohua.asia {
server 127.0.0.1:8087;
}
server {
listen 80;
server_name www.qikemi.com;
#charset koi8-r;
access_log logs/hostqikemi.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://www.qikemi.com;
include proxy.conf;
}
}
server {
listen 80;
server_name www.ady.cn;
#charset koi8-r;
access_log logs/host166801.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://www.ady.cn;
include proxy.conf;
expires 30d;
}
}
server {
listen 80;
server_name www.xiaohua.asia;
#charset koi8-r;
access_log logs/hostxiaohua.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://www.xiaohua.asia;
include proxy.conf;
expires 30d;
}
}
}
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
或者在/etc/nginx/conf.d 中直接添加自己需要配置,如下:
[root@ady_cn conf.d]# cat cas.qikemi.com.conf
upstream cas.qikemi.com {
server 127.0.0.1:8084;
}
server {
listen 80;
server_name cas.qikemi.com;
#ssl on;
#charset koi8-r;
access_log logs/cas.qikemi.com.access.log main;
### SSL cert files ###
#ssl_certificate /var/local/github/ssl/qikemi.com/1_www.qikemi.com_bundle.crt;
#ssl_certificate_key /var/local/github/ssl/qikemi.com/2_www.qikemi.com.key;
### Add SSL specific settings here ###
#keepalive_timeout 60;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://cas.qikemi.com;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
include /etc/nginx/conf.d/proxy.conf;
proxy_set_header X-Forwarded-Proto https;
}
}
[root@ady_cn conf.d]# cat shop.qikemi.com.conf
upstream shop.qikemi.com {
server 127.0.0.1:8082;
#server ady.github.io;
}
server {
listen 80;
server_name shop.qikemi.com;
#ssl on;
#charset koi8-r;
access_log logs/shop.qikemi.com.access.log main;
### SSL cert files ###
#ssl_certificate /var/local/github/ssl/qikemi.com/1_shop.qikemi.com_bundle.crt;
#ssl_certificate_key /var/local/github/ssl/qikemi.com/2_shop.qikemi.com.key;
### Add SSL specific settings here ###
#keepalive_timeout 60;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://shop.qikemi.com;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
include /etc/nginx/conf.d/proxy.conf;
proxy_set_header X-Forwarded-Proto https;
}
}
[root@ady_cn conf.d]# cat ady_cn.conf.bak
upstream www.ady.cn {
server 127.0.0.1:8081;
#server ady.github.io;
}
server {
listen 80;
server_name www.ady.cn;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 80;
server_name ady.cn;
rewrite ^(.*) https://www.ady.cn permanent;
}
server {
listen 443;
server_name www.ady.cn;
ssl on;
#charset koi8-r;
access_log logs/www.ady.cn.access.log main;
### SSL cert files ###
ssl_certificate /var/local/github/ssl/ady_cn/1_www.ady.cn_bundle.crt;
ssl_certificate_key /var/local/github/ssl/ady_cn/2_www.ady.cn.key;
### Add SSL specific settings here ###
keepalive_timeout 60;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://www.ady.cn;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
include /etc/nginx/conf.d/proxy.conf;
proxy_set_header X-Forwarded-Proto https;
}
}
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
编辑 (opens new window)
上次更新: 2024/04/19, 08:52:45
- 01
- idea 热部署插件 JRebel 安装及破解,不生效问题解决04-10
- 02
- spark中代码的执行位置(Driver or Executer)12-12
- 03
- 大数据技术之 SparkStreaming12-12