Redirect HTTP to HTTPS from Nginx is not working
12,333
Solution 1
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
.
Solution 2
I had the similar problem. It was caused by linux firewall (port 80 was disallowed).
Solution 3
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;
}
Author by
Huu Duc Cua Hang
Phân phối kinh doanh cáp hàn và lắp đặt tủ tụ bù
Updated on June 14, 2022Comments
-
Huu Duc Cua Hang almost 2 years
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.
-
danglingpointer over 6 yearsdid you enable the https support in your server config?
-
Huu Duc Cua Hang over 6 yearsYes, I did enable.
-
-
Huu Duc Cua Hang over 6 yearsI try it, and deleted duplicate code
server_name example.com
. Restart nginx but my issue is still -
Huu Duc Cua Hang over 6 years
# 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
-
nbari over 6 yearscheck that you only have one
server { listen 80 ...
you could try by first just trying the block I put here and later enable the others, usenginx -t
to test the config -
Huu Duc Cua Hang over 6 yearsCheck only in /etc/nginx/conf.d/example.com.conf ?
-
Huu Duc Cua Hang over 6 years
nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
-
Admin over 2 yearsPlease add further details to expand on your answer, such as working code or documentation citations.