简体   繁体   中英

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).

WordPress Address (URL) and Site Address (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;

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 .

I had the similar problem. It was caused by linux firewall (port 80 was disallowed).

Your configuration is not clear, you have at the end a duplicated server_name example.com line.

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;

With return 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

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:

Say you enter http://www.example.com , then it will redirect to https://www.example.com
, if someone enters http:example.com, it will redirect to 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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