简体   繁体   English

将 HTTP 从 Nginx 重定向到 HTTPS 无效

[英]Redirect HTTP to HTTPS from Nginx is not working

Type: https://example.com => ssl ok But type: www.example.com and example.com is http no redirect https. (www redirect to non-www).输入: https://example.com => ssl ok但是输入:www.example.com 和 example.com 是 http没有重定向到 881056383.5270。

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

/etc/nginx/conf.d/example.com.conf /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;

How to fix it?如何解决? Sorry my bad English, thank you.抱歉我的英语不好,谢谢。

This might be caused by the ambiguous server name indeed.这可能确实是由不明确的服务器名称引起的。 Try using the following:尝试使用以下方法:

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;
    }
}

You can check your nginx configuration by running sudo nginx -t .您可以通过运行sudo nginx -t来检查您的 nginx 配置。

I had the similar problem.我有类似的问题。 It was caused by linux firewall (port 80 was disallowed).它是由 linux 防火墙引起的(80 端口被禁止)。

Your configuration is not clear, you have at the end a duplicated server_name example.com line.您的配置不清楚,最后您有一个重复的server_name example.com行。

Try to use this:尝试使用这个:

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

Update this line return 301 https://$server_name$request_uri;更新这一行 return 301 https://$server_name$request_uri;

With return 301 https://$http_host$request_uri返回 301 https://$http_host$request_uri

i have also the same problem with nginx so I apply these to server to accept only one request which will decide for both http and https我也有与 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;
    }
}

by using this I have only server block to hanlde any request通过使用这个我只有服务器块来处理任何请求

This code will help you redirect to https:此代码将帮助您重定向到 https:

Say you enter http://www.example.com , then it will redirect to https://www.example.com假设您输入http://www.example.com ,然后它将重定向到https://www.example.com
, if someone enters http:example.com, it will redirect to 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM