nginx-端口多域名转发

nginx 监听一个端口多域名转发配置

www.eting.info / blog.eting.info 同时指向同一台服务器不同端口

www.eting.info -> 45.77.151.91:8080
blog.eting.info -> 45.77.151.91:9090

配置文件

1
2
3
4
5
6
7
➜  ~ whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
➜ ~ cd /etc/nginx
➜ nginx ls
conf.d koi-utf nginx.conf sites-available uwsgi_params
fastcgi.conf koi-win proxy_params sites-enabled win-utf
fastcgi_params mime.types scgi_params snippets

找到 nginx.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
➜  nginx cat nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;
}

查看 -> include /etc/nginx/conf.d/.conf;
大概意思 -> 加载include /etc/nginx/conf.d;这个文件里面所有.conf配置文件

进入 conf.d文件 创建 blog.conf error.conf www.conf

1
➜  nginx cd conf.d

blog.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name blog.eting.info;

location / {
proxy_pass http://localhost:9090;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root html;
index index.html index.htm;
}
}

error.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name 45.77.151.91;
error_page 404 401 400 /400.html;

location / {
root html;
index 400.html;
}

location = /40x.html{
root html;
}
}

www.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name www.eting.info;

location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root html;
index index.html index.htm;
}
}

重启nginx

1
➜  conf.d nginx -s reload

ok


同一个文件配置 把server 都写在nginx.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
67
68
69
70
71
72
73
74
75
#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 1024;
}


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 65;

#gzip on;
#用服务器IP不能访问
 server {
        listen       80;
        server_name 182.92.xx.xx;
        error_page 404 401 400  /400.html;

        location / {             
            root   html;
            index 400.html;        
        }
    
        location = /40x.html{
            root html;
        }
    }

  server {
listen 80;
server_name xxx1.com.cn www.xxx1.com.cn;

location / {
proxy_pass http://localhost:8091;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name xxx2.com www.xxx2.com;

location / {
proxy_pass http://localhost:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root html;
index index.html index.htm;
}
}
}