HelloWood

Nginx 配置

2018-01-01

1. 基础配置

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
worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {
listen 80;
server_name project.hellowood.com;

access_log logs/project.access.log;

location / {
proxy_pass http://127.0.0.1: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;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

访问project.hellowood.com 实际会访问该 IP 下的8080端口

2. 不同的 URL 访问不同的服务器

  • 配置 Server 节点
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
server {
listen 80;
server_name project.hellowood.com;

access_log logs/project.access.log;

location / {
proxy_pass http://127.0.0.1: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;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 80;
server_name introduce.hellowood.com;

access_log logs/introduce.access.log;

location / {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 80;
server_name helloworld.com;

access_log logs/helloworld.access.log;

location / {
proxy_pass http://127.0.0.1:8082;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

访问project.hellowood.com 实际会访问该 IP 下的8080端口
访问introduce.hellowood.com 实际会访问该 IP 下的8081端口
访问helloworld.com 实际会访问该 IP 下的8082端口


3. 不同的项目路径访问不同的服务器

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
server {  
listen 80;
server_name hellowood.com;
location / {
root html;
index index.html index.htm;
}
location /helloworld {
proxy_pass http://192.168.101.101:8080/helloworld;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /hi {
proxy_pass http://192.168.101.101:8080/hi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /howareyou {
proxy_pass http://192.168.101.101:8088/howareyou;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /introduce {
proxy_pass http://10.2.10.11:9999/introduce;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /lalala {
proxy_pass http://www.lalala.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

访问hellowood.com 实际会访问该 IP 下的 Nginx 目录下的静态页面
访问hellowood.com/helloworld 实际会访问http://192.168.101.101:8080/helloworld
访问hellowood.com/hi 实际会访问http://192.168.101.101:8080/hi
访问hellowood.com/howareyou 实际会访问http://192.168.101.101:8088/howareyou
访问hellowood.com/introduce 实际会访问http://10.2.10.11:9999/introduce
访问hellowood.com/lalala 实际会访问http://www.lalala.com

Tags: Nginx