小工具知识网

当前位置: 首页 > 行业知识 > Nginx将http重定向到https,一直提示重定向次数过多(已解决)

行业知识

Nginx将http重定向到https,一直提示重定向次数过多(已解决)

2026-11-05 11:11:26 来源:互联网转载

先贴一下nginx的配置

http {    include       mime.types;    default_type  application/octet-stream;    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';    #access_log  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    server {       listen     80;       server_name e.com;       # rewrite ^(.*) https://$server_name permanent;       return 301 https://$host$request_uri;   # gzip config   gzip on;   gzip_min_length 1k;   gzip_comp_level 9;   gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;   gzip_vary on;   gzip_disable "MSIE [1-6]\.";   root /opt/hengze/dist;        location / {          root /dist;          index  index.html;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}location ^~/api/ {        proxy_pass https://e.com/; #修改此地址为后台服务地址        proxy_set_header X-Forwarded-Proto $scheme;        proxy_set_header Host $host:$server_port;        proxy_set_header X-Real-IP  $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_http_version 1.1;        proxy_set_header Upgrade $http_upgrade;        proxy_set_header Connection "upgrade";        proxy_connect_timeout   1;        proxy_buffering off;        chunked_transfer_encoding off;        proxy_cache off;        proxy_send_timeout      30m;        proxy_read_timeout      30m;        client_max_body_size    100m;    }    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    server {        listen       443 ssl;        server_name  e.com;        ssl_certificate      cert/e.com.pem;        ssl_certificate_key  cert/e.com.key;        ssl_session_cache    shared:SSL:1m;        ssl_session_timeout  5m;        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;        ssl_prefer_server_ciphers  on;                location / {#        解决wesock问题proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-IP $remote_addr;#从这里往下三行处理前后端服务在一个服务器中        proxy_set_header X-Real-IP $remote_addr;         proxy_set_header Host $http_host;         proxy_pass http://localhost;        }    }}

return和rewrite都已经试过了,都是多次重定向导致无法进入页面

后续在https区域中将连接后端地址的api区域复制过来后,再加上下方代码完美解决

#将所有HTTP请求通过rewrite指令重定向到HTTPS。

rewrite ^(.*)$ https://$host$1;

上一篇:微企点建站怎么样?

下一篇:解决电脑自动关机难题,详细教程助你轻松操作!