关于nginx:nginx各种代理配置

0次阅读

共计 3773 个字符,预计需要花费 10 分钟才能阅读完成。

记录 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;
    }
  }
}
正文完
 0