nginx various proxy configurations


提示:记录nginx不同访问路径和代理的配置,注意有加斜杠和不加的区别。

The following shows the configuration under different circumstances: location path, root path, alias path, proxy_pass proxy path.
By comparing these configuration path addresses, it is recommended to include a slash after the location.

1. Basic configuration instructions

# 进程数量
worker_processes 1;

events {
  # 最大连接数量
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;

2. Demonstrate how to force http to jump to https

server {
  listen 80;
  server_name test.com;

  # http强制跳转到https
  rewrite ^(.*)$ https://$server_name$1 permanent;
}

3. Demonstrate how to configure the verification file of WeChat payment

server {
  listen 80;
  server_name localhost;

  # 默认根路径
  location / {
    root index.html;
  }
  # 微信支付校验文件,可以直接配置访问名称
  location ^~/MP_verify_2g3uEjrB5B2LIbNl.txt {
    alias /home/MP_verify_2g3uEjrB5B2LIbNl.txt;
  }
  # 微信支付校验文件,也可以通过正则配置
  location ~^/MP_verify_[a-zA-Z0-9]*\.(txt)$ {
    root /home/;
    rewrite ^/home/(.txt)$ /home/$1 last;
  }
}

4. Demonstrate the difference between root and alias for configuring static resources.

server {
  listen 80;
  server_name localhost;

  # 用root方式,location中的路径会拼加到root的地址后面
  # 请求路径为:http://localhost:8080/files/index.jpg    实际访问为:/home/files/index.jpg
  location ~^/files/ {
    root /home/;
    index index.html index.htm;
  }
  # 用alias方式,location中的路径不会拼加到alias的地址后面
  # 这请求路径为:http://localhost:8080/files/index.jpg    实际访问为:/home/index.jpg
  location ~^/files/ {
    alias /home/;
    index index.html index.htm;
  }
}

5. Demo request background interface proxy configuration

server {
  listen 8080;
  server_name localhost;

  #################### 第一种场景(代理地址不加斜杠) ####################
  # 请求路径为:http://127.0.0.1:8080/api/getUser   实际代理为:http://127.0.0.1:8000/api/getUser
  location ^~/api/ {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
  }
  # 请求路径为:http://127.0.0.1:8080/api/getUser   实际指向为:http://127.0.0.1:8000/api/getUser
  location ^~/api {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
  }

  #################### 第二种场景(代理地址+斜杠) ####################
  # 请求路径为:http://127.0.0.1:8080/api/getUser   实际代理为:http://127.0.0.1:8000/getUser
  location ^~/api/ {
    proxy_pass http://127.0.0.1:8000/;
    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
  }
  # 请求路径为:http://127.0.0.1:8080/api/getUser   实际代理为:http://127.0.0.1:8000//getUser
  location ^~/api {
    proxy_pass http://127.0.0.1:8000/;
    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
  }

  #################### 第三种场景(代理地址+后缀) ####################
  # 请求路径为:http://127.0.0.1:8080/api/getUser   实际代理为:http://127.0.0.1:8000/user/getUser
  location ^~/api {
    proxy_pass http://127.0.0.1:8000/user;
    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
  }
  # 请求路径为:http://127.0.0.1:8080/api/getUser   实际代理为:http://127.0.0.1:8000/usergetUser
  location ^~/api/ {
    proxy_pass http://127.0.0.1:8000/user;
    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
  }

  #################### 第四种场景(代理地址+后缀+斜杠) ####################
  # 请求路径为:http://127.0.0.1:8080/api/getUser   实际代理为:http://127.0.0.1:8000/user/getUser
  location ^~/api/ {
    proxy_pass http://127.0.0.1:8000/user/;
    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
  }
  # 请求路径为:http://127.0.0.1:8080/api/getUser   实际代理为:http://127.0.0.1:8000/user//getUser
  location ^~/api {
    proxy_pass http://127.0.0.1:8000/user/;
    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
  }
}

6. Demonstrate how to deploy nginx for front-end projects

server {
  listen 8090;
  server_name localhost;

  # 默认访问
  # 部署路径:/home/web/my_demo
  # 访问路径为:http://localhost:8090/
  location / {
    try_files $uri $uri/ /index.html;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    root /home/web/my_demo/;
    index index.html index.htm;
  }

  # 带前缀的访问
  # 部署路径:/home/web/my_demo
  # 访问路径为:http://localhost:8090/my_demo/
  # 如果location路径最后没有配置斜杠,则浏览器输入访问地址后,路径最后会自动拼一个斜杠
  location ^~/my_demo/ {
    try_files $uri $uri/ /my_demo/index.html;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    root /home/web/;
    index index.html index.htm;
  }
}
}

Guess you like

Origin blog.csdn.net/bacawa/article/details/129105125