重定向nginx导致“太多重定向”错误

3xiyfsfu  于 2023-01-29  发布在  Nginx
关注(0)|答案(1)|浏览(273)

我需要用nginx将www重定向到非www,将HTTP重定向到HTTPS。我可以让它重定向,但是我得到了一个“太多重定向”错误。
我使用的是Azure AppService版本的WordPress,这个版本使用wordpress-alpine-php docker映像,运行的是nginx版本1.20.2。
nginx.conf文件包括:

  • /etc/nginx/配置文件d/*.配置文件
  • /etc/nginx/已启用模块/*.conf

我没有看到启用模块的目录。
对于HTTP到https的重定向,我在default.conf中添加了以下服务器指令:

server {
    listen 80;
    server_name ---.com www.---.com;
    return 301 https://---.com$request_uri;
}

在此之后,我得到了“太多重定向”错误。
我注意到下面的服务器块也监听端口80,所以我把它改成了443。我仍然得到了“太多的重定向”。
下面是我的conf文件,我做的唯一更改是在上面添加了server指令,并在原来的server指令中将端口改为443。
我如何让这些重定向工作?
会不会还有其他文件?

/等文件/nginx/nginx配置文件

user  nginx;
worker_processes  auto;

# send nginx error logs to stderr
error_log  /dev/stderr error;
pid        /var/run/nginx.pid;
load_module modules/ngx_http_brotli_static_module.so;
load_module modules/ngx_http_brotli_filter_module.so;

events {
    worker_connections  10000;
}

http {
    include       /etc/nginx/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  off;
    sendfile    on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/modules-enabled/*.conf;
}

/etc/nginx/配置文件d/默认配置文件

upstream php {
        server unix:/var/run/php/php-fpm.sock;        
        #server 127.0.0.1:9000;
}
server {
    listen 80;
    server_name ---.com www.---.com;
    return 301 https://---.com$request_uri;
}
server {
        listen 443;
        ## Your website name goes here.
        server_name _;
        if ($http_x_forwarded_proto = "http") {
            return 301 https://---.com$request_uri;
        }
        ## Your only path reference.
        root /home/site/wwwroot;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        # Add locations of phpmyadmin here.
        location /phpmyadmin {
                root /home/;
                index index.php index.html index.htm;
                location ~ ^/phpmyadmin/(.+\.php)$ {
                        try_files $uri =404;
                        root /home/;
                        fastcgi_pass unix:/var/run/php/php-fpm.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include /etc/nginx/fastcgi_params;
                }
                location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                        root /home/;
                }
        }

        location /phpMyAdmin {
                rewrite ^/* /phpmyadmin last;
        }

  # Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
  sendfile off;

  set $skip_cache 0;

  # POST requests and urls with a query string should always go to PHP
  if ($request_method = POST) {
    set $skip_cache 1;
  }
  if ($query_string != "") {
    set $skip_cache 1;
  }

  # Don't cache uris containing the following segments
  if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
    set $skip_cache 1;
  }

  # Don't use the cache for logged in users or recent commenters
  if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
  }

  # Don't cache WooCommerce URLs
  # Cart widgets are still a problem: https://github.com/emcniece/docker-wordpress/issues/3
  if ($request_uri ~* "/(cart|checkout|my-account)/*$") {
    set $skip_cache 1;
  }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~* \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                include fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass php;

                fastcgi_read_timeout 300;
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                fastcgi_cache off;
                fastcgi_cache_valid 60m;                
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}
8ehkhllq

8ehkhllq1#

如果你想使用HTTPS,你需要在你的listen指令中添加ssl标志,并指定你的SSL证书-类似于:

server {
  listen 443 ssl http2;
  server_name ---.com;
  
        ssl_session_cache shared:SSL:4m; # measured in megabytes, not minutes
        ssl_buffer_size 4k; # reduced from the default 16k to minimize TTFB
        ssl_session_timeout 30m;
        ssl_session_tickets on; # Requires nginx >= 1.5.9 (SSL labs testing leads to SSL: error:14094085:SSL routines:SSL3_READ_BYTES:ccs received early)
        ssl_dhparam /etc/ssl/dhparam.pem; # Generate with "openssl dhparam -out dhparam.pem 4096"
        ssl_prefer_server_ciphers on;
        ssl_protocols TLSv1.3 TLSv1.2;

        ssl_stapling on;  
        ssl_stapling_verify on;  

        resolver 1.1.1.1 valid=300s ipv6=off;  
        resolver_timeout 4s;

        ssl_certificate /etc/ssl/fullchain.pem;
        ssl_certificate_key /etc/ssl/key.pem;

  .......
}

相关问题