【问题标题】:Redirect HTTP to HTTPS from Nginx is not working从 Nginx 将 HTTP 重定向到 HTTPS 不起作用
【发布时间】:2018-03-27 16:14:45
【问题描述】:

类型:https://example.com => ssl ok 但是输入:www.example.com 和 example.com 是 http no redirect https。 (www 重定向到非 www)。

WordPress 地址(URL)和站点地址(URL):https//example.com

/etc/nginx/conf.d/example.com.conf

server {
listen 80; 
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl on;
ssl_certificate /etc/nginx/ssl/cert_chain.crt;
#ssl_CACertificate_File /etc/nginx/ssl/example.com.ca-bundle;
ssl_certificate_key /etc/nginx/ssl/example.com.key; 
access_log off;
# access_log /home/example.com/logs/access_log;
error_log off;
# error_log /home/example.com/logs/error.log; 
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
root /home/example.com/public_html;
include /etc/nginx/conf/ddos2.conf;
index index.php index.html index.htm;
server_name example.com;

如何解决? 对不起,我的英语不好,谢谢。

【问题讨论】:

  • 您是否在服务器配置中启用了 https 支持?
  • 是的,我确实启用了。

标签: ssl nginx https


【解决方案1】:

我对 nginx 也有同样的问题,所以我将这些应用到服务器以只接受一个请求,该请求将决定 http 和 https

server { 
    listen 80 ;
    listen [::]:80 ;
    listen 443 ssl http2 ;
    listen [::]:443 ssl http2 ;
    server_name example.com www.example.com; 

    #------ ssl certificates and other config --------

    set $https_redirect 0;
    #if request came from port 80/http
    if ($server_port = 80) { 
        set $https_redirect 1; 
    }
    # or if the requested host came with www 
    if ($host ~ '^www\.') { 
        set $https_redirect 1; 
    }
    #then it will redirects
    if ($https_redirect = 1) {
        return 301 https://example.com$request_uri;
    }
}

通过使用这个我只有服务器块来处理任何请求

【讨论】:

    【解决方案2】:

    更新此行返回 301 https://$server_name$request_uri;

    返回 301 https://$http_host$request_uri

    【讨论】:

    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    【解决方案3】:

    此代码将帮助您重定向到 https:

    假设你输入http://www.example.com,那么它会重定向到https://www.example.com
    , 如果有人进入http:example.com,它将重定向到https://www.example.com

    <VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /usr/local/apache2/htdocs
    Redirect permanent / https://www.example.com/
    </VirtualHost>
    <VirtualHost_default_:443>
    ServerName www.example.com
    DocumentRoot /usr/local/apache2/htdocs
    SSLEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
    RewriteRule (.*) https://www.example.com/$1 [R=301,L]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^example\.com$
    RewriteRule (.*) https://www.example.com/$1 [R=301,L]
    </VirtualHost>
    

    【讨论】:

      【解决方案4】:

      我遇到了类似的问题。它是由 linux 防火墙引起的(端口 80 被禁止)。

      【讨论】:

        【解决方案5】:

        这确实可能是由不明确的服务器名称引起的。尝试使用以下内容:

        server {
        
            server_name example.com www.example.com;
            listen 80;
            listen 443 ssl; # Listen for SSL at port 443 as well
        
            # ... other config - certificates and such
        
            # If a user tries to come through http, redirect them through https
            if ($scheme != "https") { 
                return 301 https://$host$request_uri;
            }
        }
        

        你可以通过运行sudo nginx -t来检查你的nginx配置。

        【讨论】:

          【解决方案6】:

          您的配置不清楚,最后有重复的server_name example.com 行。

          尝试使用这个:

          server {
              listen 80 default_server;
              listen [::]:80 default_server;
              server_name example.com www.example.com;
              return 301 https://$server_name$request_uri;
          }
          

          【讨论】:

          • 我试了一下,删除了重复的代码server_name example.com。重启 nginx 但我的问题依旧
          • # service nginx restart nginx: [warn] conflicting server name "example.com" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "www.example.com" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "example.com" on 0.0.0.0:80, ignored Stopping nginx: [ OK ] Starting nginx: nginx: [warn] conflicting server name "example.com" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "www.example.com" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "example.com" on 0.0.0.0:80, ignored
          • 检查您是否只有一个 server { listen 80 ...,您可以先尝试我放在这里的块,然后启用其他块,使用 nginx -t 测试配置
          • 只检查 /etc/nginx/conf.d/example.com.conf ?
          • nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
          猜你喜欢
          • 2014-08-14
          • 2022-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-29
          • 2018-01-05
          相关资源
          最近更新 更多