Redirect Nginx将所有重定向到https和非www-重定向循环

Redirect Nginx将所有重定向到https和非www-重定向循环,redirect,nginx,https,rewrite,Redirect,Nginx,Https,Rewrite,我正在尝试将服务器名称的所有非http和www流量重定向到。我有以下问题: 任何请求都不会被重定向 对http://example.com的任何请求都会得到错误400 任何https请求都会进入301重定向循环 我尝试过使用不同的服务器块的多种变体,最近的一次尝试是让所有的功能都能正常工作,除了重定向循环,无论我做什么,它都会困扰我。如果您能给我一些建议和建议,我将不胜感激。代码如下: server { listen 443; listen 80;

我正在尝试将服务器名称的所有非http和www流量重定向到。我有以下问题:

  • 任何请求都不会被重定向
  • 对http://example.com的任何请求都会得到错误400
  • 任何https请求都会进入301重定向循环
  • 我尝试过使用不同的服务器块的多种变体,最近的一次尝试是让所有的功能都能正常工作,除了重定向循环,无论我做什么,它都会困扰我。如果您能给我一些建议和建议,我将不胜感激。代码如下:

    server {
       listen         443;
       listen         80;
       server_name    www.example.com example.com;
    
    root /home/example/public_html;
    index index.html index.htm index.php;
    
    #SSL Stuff here
    
    if ($scheme = http) { return 301 https://example.com$request_uri; }
    if ($server_name = www.example.com) { return 301 https://example.com$request_uri; }
    
    include conf.d/wp/restrictions.conf;
    include conf.d/wp/wordpress.conf;
    }
    
    编辑 因此,我尝试了下面的方法,除了http://www.example.com被允许在不重定向到SSL的情况下通过之外,没有301循环。我不明白这是怎么可能的,因为它应该被80号端口规则抓住不?更新配置如下:

    server {
    listen         80;
       server_name    example.com;
       server_name    www.example.com;
       return 301 https://$server_name$request_uri;
    }
    
    ################# SECURE #################
    
    server {
    listen   443;
    server_name example.com;
    access_log /var/log/nginx/example-ssl.access.log;
    error_log /var/log/nginx/example-ssl.error.log;
    
    root /home/example/public_html;
    index index.html index.htm index.php;
    
    # SSL Stuff here
    
    #include conf.d/wp/restrictions.conf;
    #include conf.d/wp/wordpress.conf;
    }
    

    您的配置存在一些问题

  • 我将替换
    $host
    $server\u name
    ,或者对域进行硬编码 您需要,比如“
    secondexample.com$request\u uri
  • 您缺少
    server 443
    行中的
    ssl
    标记
  • 配置:

    server {
       listen         80;
       server_name    example.com;
       server_name    www.example.com;
       return 301 https://$host$request_uri;
    }
    
    ################# SECURE #################
    
    server {
       listen   443 ssl;
       server_name example.com;
       access_log /var/log/nginx/example-ssl.access.log;
       error_log /var/log/nginx/example-ssl.error.log;
    
       root /home/example/public_html;
       index index.html index.htm index.php;
    
       # SSL Stuff here
    
       #include conf.d/wp/restrictions.conf;
       #include conf.d/wp/wordpress.conf;
    }
    

    您的配置存在一些问题

  • 我将替换
    $host
    $server\u name
    ,或者对域进行硬编码 您需要,比如“
    secondexample.com$request\u uri
  • 您缺少
    server 443
    行中的
    ssl
    标记
  • 配置:

    server {
       listen         80;
       server_name    example.com;
       server_name    www.example.com;
       return 301 https://$host$request_uri;
    }
    
    ################# SECURE #################
    
    server {
       listen   443 ssl;
       server_name example.com;
       access_log /var/log/nginx/example-ssl.access.log;
       error_log /var/log/nginx/example-ssl.error.log;
    
       root /home/example/public_html;
       index index.html index.htm index.php;
    
       # SSL Stuff here
    
       #include conf.d/wp/restrictions.conf;
       #include conf.d/wp/wordpress.conf;
    }