从Nginx重定向HTTP到HTTPS不起作用

mkh04yzy  于 5个月前  发布在  Nginx
关注(0)|答案(7)|浏览(119)

类型:https://example.com =>ssl ok但类型:www.example.com和example.com是httpno redirecthttps.(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;

字符串
怎么修?抱歉我英语不好,谢谢。

x6yk4ghg

x6yk4ghg1#

这可能是由于服务器名称不明确造成的。请尝试使用以下命令:

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配置。

gmol1639

gmol16392#

我也遇到过类似的问题。这是由Linux防火墙引起的(端口80被禁止)。

kg7wmglp

kg7wmglp3#

您的配置不清楚,您在末尾有一个重复的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;
}

字符串

h7appiyu

h7appiyu4#

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

brc7rcf0

brc7rcf05#

我也有同样的问题与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;
    }
}

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

ygya80vv

ygya80vv6#

如果您的域设置位于

/etc/nginx/sites-available/default

字符串
或者你可以在/etc/nginx/sites-available/domain.com上为每个域名设置不同的文件。

server {
    if ($host = www.domainname1.com) {
        return 301 https://$host$request_uri;
    }
    if ($host = www.domainname2.com) {
            return 301 https://$host$request_uri;
    }
    listen 80;
    listen [::]:80;

    server_name www.domainname1.com www.domainname2.com;
    return 404;
}

vkc1a9a2

vkc1a9a27#

这段代码将帮助你重定向到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>

字符串

相关问题