V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
kuanos
V2EX  ›  问与答

nginx 的重定向问题(小白提问)

  •  
  •   kuanos · 2020-11-18 10:00:44 +08:00 · 1076 次点击
    这是一个创建于 1226 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我自己弄了个 wordpress,用 nginx 配置两个不同的目录分别作为不同的站点,其中一个正常访问,另一个配置好了之后,ssl 也生效了,但是就是会显示重定向次数过多,我怀疑是强制 80 转 443 的问题,但是 nginx-t,以及 status 都没有报错。

    小白找不到问题,求解答,非常感谢:

    server {

    listen 443 ssl;
    listen 80;

    server_name ABC.com www.ABC.com;
    client_max_body_size 500m;

    root /var/www/wordpress/;
    index index.php index.html index.htm index.nginx-debian.html;

    #if ($scheme = http ) {return 301 https://$host$request_uri;}

    ssl_certificate /etc/letsencrypt/live/ABC.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ABC.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/ABC.com/chain.pem;
    include snippets/ssl.conf;
    return 301 https://ABC.com$request_uri;
    include snippets/letsencrypt.conf;

    location / {
    # try_files $uri $uri/ =404;
    try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
    deny all;
    }
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
    expires max;
    log_not_found off;
    }
    }
    4 条回复    2020-11-18 13:32:23 +08:00
    jifengg
        1
    jifengg  
       2020-11-18 11:00:32 +08:00
    你这不管三七二十一全部 return 301,能不重定向次数过多吗?
    我一个服务器是这么设置的:

    #设置域名跳转 https
    server{
    listen 80;
    server_name www.example.com;
    return 301 https://$host$request_uri;
    }

    80 和 443 端口监听分开,简单易懂
    WeKeey
        2
    WeKeey  
       2020-11-18 11:10:13 +08:00
    server {
    listen 80;
    server_name xxx.xyz;
    return 301 https://xxx.xyz$request_uri;
    }

    server {
    listen 443 ssl;
    server_name xxx.xyz;

    ssl_certificate cert/xyz.xxx.crt;
    ssl_certificate_key cert/xyz.xxx.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
    proxy_pass http://zzz;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    access_log /var/log/nginx/xxx.access.log;
    }
    kuanos
        3
    kuanos  
    OP
       2020-11-18 13:28:18 +08:00
    @WeKeey @jifengg 感谢,已经解决了
    quanjw
        4
    quanjw  
       2020-11-18 13:32:23 +08:00
    你试试 497 状态码? 但是 我好像不行 也没找到原因、、、
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1319 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:32 · PVG 07:32 · LAX 16:32 · JFK 19:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.