[英]Redirect HTTP to HTTPS from Nginx is not working
輸入: https://example.com => ssl ok但是輸入:www.example.com 和 example.com 是 http沒有重定向到 881056383.5270。
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;
如何解決? 抱歉我的英語不好,謝謝。
這可能確實是由不明確的服務器名稱引起的。 嘗試使用以下方法:
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 配置。
我有類似的問題。 它是由 linux 防火牆引起的(80 端口被禁止)。
您的配置不清楚,最后您有一個重復的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;
}
更新這一行 return 301 https://$server_name$request_uri;
返回 301 https://$http_host$request_uri
我也有與 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;
}
}
通過使用這個我只有服務器塊來處理任何請求
此代碼將幫助您重定向到 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.