关于nginx:nginx各种代理配置

记录nginx不同拜访门路和代理的配置,留神有加斜杠和不加的区别

以下展现不同状况下的配置:location门路、root门路、alias门路、proxy_pass代理门路。
通过这几个配置门路地址比照,倡议location前面都带上斜杠。
# 过程数量
worker_processes 1;

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

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

  # 演示如何强制http跳转https
  server {
    listen 80;
    server_name test.com;

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

  # 演示如何配置微信领取的校验文件
  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;
    }
  }

  # 演示root和alias两种配置动态资源的区别
  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;
    }
  }

  # 演示申请后盾接口代理配置
  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地址
    }
  }

  # 演示前端我的项目如何部署nginx
  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;
    }
  }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理