# nginx 配置

# 1.代理地址不加/

server {
  listen 8080;
  server_name localhost;
  # http:localhost:8080/api/getuser --> proxy_pass + /api/getuser --> http:localhost:8000/api/getuser
  location ^~/api/ {
    proxy_pass http://localhost:8000;
    proxy_set_header Host $http_host; #后端可以获取完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #可以拿到用户访问的ip
  }
  # http:localhost:8080/api/getuser --> proxy_pass + /api/getuser --> http:localhost:8000/api/getuser
  location ^~/api {
    proxy_pass http://localhost:8000;
    proxy_set_header Host $http_host; #后端可以获取完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #可以拿到用户访问的ip
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 2.代理地址加/或者有其他内容,先替换后拼接

server {
  listen 8080;
  server_name localhost;
  # http:localhost:8080/api/getuser --> proxy_pass + getuser --> http:localhost:8000/getuser
  location ^~/api/ {
    proxy_pass http://localhost:8000/;
    proxy_set_header Host $http_host; #后端可以获取完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #可以拿到用户访问的ip
  }
  # http:localhost:8080/api/getuser --> proxy_pass + /getuser --> http:localhost:8000//getuser
  location ^~/api {
    proxy_pass http://localhost:8000/;
    proxy_set_header Host $http_host; #后端可以获取完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #可以拿到用户访问的ip
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16