nginx反向代理http和https共同使用 双存在

[复制链接]
ordersy 发表于 2019-7-1 07:25:52 | 显示全部楼层 |阅读模式
今天发现了一个问题,就是在反代过程中,https跟http只能单一反代!
不能自适应协议,也不支持协议变量,各种百度啊,两个钟头,测试了各种,都不适用宝塔,
第一种就是建两个server,80与443端口分开。
不过这样代码比较长,不利于维护,第二种,加一个判断,比较简单,宝塔需要手动修改,不能傻瓜设置!
我下面只贴主要代码!
第一种
  1. server
  2. {
  3.     listen 80;
  4.     server_name www.yeguobiji.com yeguobiji.com;
  5.     index index.php index.html index.htm default.php default.htm default.html;
  6.     root /www/wwwroot/www.yeguobiji.com;

  7.     location /
  8.     {
  9.         proxy_pass http://www.yeguobiji.com;
  10.         proxy_set_header Host $host;
  11.         proxy_set_header X-Real-IP $remote_addr;
  12.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  13.         proxy_set_header REMOTE-HOST $remote_addr;
  14.                
  15.         #持久化连接相关配置
  16.         #proxy_connect_timeout 30s;
  17.         #proxy_read_timeout 86400s;
  18.         #proxy_send_timeout 30s;
  19.         #proxy_http_version 1.1;
  20.         #proxy_set_header Upgrade $http_upgrade;
  21.         #proxy_set_header Connection "upgrade";
  22.         
  23.         add_header X-Cache $upstream_cache_status;
  24.         
  25.         expires 12h;
  26.     }

  27. }
  28. server
  29. {
  30.     listen 443 ssl http2;
  31.     server_name www.yeguobiji.com yeguobiji.com;
  32.     index index.php index.html index.htm default.php default.htm default.html;
  33.     root /www/wwwroot/www.yeguobiji.com;
  34.    
  35.     #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
  36.     #error_page 404/404.html;
  37.     ssl_certificate    /etc/letsencrypt/live/www.yeguobiji.com/fullchain.pem;
  38.     ssl_certificate_key    /etc/letsencrypt/live/www.yeguobiji.com/privkey.pem;
  39.     ssl_protocols TLSv1.1 TLSv1.2;
  40.     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  41.     ssl_prefer_server_ciphers on;
  42.     ssl_session_cache shared:SSL:10m;
  43.     ssl_session_timeout 10m;
  44.     error_page 497  https://$host$request_uri;

  45.     location /
  46.     {

  47.         proxy_pass https://www.yeguobiji.com;

  48.         proxy_set_header Host $host;
  49.         proxy_set_header X-Real-IP $remote_addr;
  50.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  51.         proxy_set_header REMOTE-HOST $remote_addr;
  52.                
  53.         #持久化连接相关配置
  54.         #proxy_connect_timeout 30s;
  55.         #proxy_read_timeout 86400s;
  56.         #proxy_send_timeout 30s;
  57.         #proxy_http_version 1.1;
  58.         #proxy_set_header Upgrade $http_upgrade;
  59.         #proxy_set_header Connection "upgrade";
  60.         
  61.         add_header X-Cache $upstream_cache_status;
  62.         
  63.         expires 12h;
  64.     }
  65. }
复制代码
  1. server
  2. {
  3.     listen 80;
  4.     listen 443 ssl http2;
  5.     server_name www.yeguobiji.com yeguobji.com;
  6.     index index.php index.html index.htm default.php default.htm default.html;
  7.     root /www/wwwroot/www.yeguobiji.com;

  8.     location /
  9.     {
  10.         if ($server_port = 80){
  11.             proxy_pass http://www.yeguobiji.com;
  12.         }
  13.         if ($server_port = 443){
  14.             proxy_pass https://www.yeguobiji.com;
  15.         }
  16.         proxy_set_header Host $host;
  17.         proxy_set_header X-Real-IP $remote_addr;
  18.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  19.         proxy_set_header REMOTE-HOST $remote_addr;
  20.                
  21.         #持久化连接相关配置
  22.         #proxy_connect_timeout 30s;
  23.         #proxy_read_timeout 86400s;
  24.         #proxy_send_timeout 30s;
  25.         #proxy_http_version 1.1;
  26.         #proxy_set_header Upgrade $http_upgrade;
  27.         #proxy_set_header Connection "upgrade";
  28.         
  29.         add_header X-Cache $upstream_cache_status;
  30.         
  31.         expires 12h;
  32.     }

  33. }
复制代码


啦啦啦
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则