强制www。和 nginx.conf 中的 https (SSL)

Force www. and https in nginx.conf (SSL)

购买 SSL 证书后,我一直在尝试强制所有页面都使用安全的 https 和 www。

https://www.exampl.com 有效且安全,但前提是准确输入。 www.example.com 或 example.com 仍指向 http。

我们使用nginx作为代理,需要在那里输入rewrite。我可以通过 Putty 进行 SSH / root 访问。我已经通过输入 putty 访问了 nginx.conf。

现在呢?我是否在此页面上输入 nginx 命令?从光标所在的位置开始?先有命令行吗?

HTTPS:

.htacess – 在我发现我必须输入到 nginx 之前得到的原始代码

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/ [R,L]

Nginx code converter – 转换器上是这样显示的。一切都在正确的线上吗?

# nginx configuration location / {
if ($http_host ~* "^example.com"){
rewrite ^(.*)$ http://example.com/ redirect; } }

然后是

WWW

.htacess – 在我发现我必须输入到 nginx 之前得到的原始代码

#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/ [L,R=301,NC]

Nginx code converter – 转换器上是这样显示的。一切都在正确的线上吗?

# nginx configuration location / { 
if ($http_host ~* "^example.com"){ 
rewrite ^(.*)$ http://www.example.com/ redirect; } 

}

然后我保存吗?重新开始?

如有任何帮助,我们将不胜感激。我已经为此奋斗了数周。我的托管公司尽其所能提供帮助,现在我正在学习……。还是我应该停下来聘请开发人员? $$$

谢谢

实现 WWW 和 HTTPS 重定向的最佳方法是在 Nginx 配置中创建一个新的 server 部分:

server {
    listen      80;   #listen for all the HTTP requests
    server_name example.com www.example.com;
    return      301         https://www.example.com$request_uri;
}

您还必须执行 https://example.com to https://www.example.com 重定向。这可以使用类似于以下的代码来完成:

server {
    listen              443 ssl;
    server_name         example.com;

    ssl_certificate     ssl.crt; #you have to put here...
    ssl_certificate_key ssl.key; #   ...paths to your certificate files
    return      301     https://www.example.com$request_uri;
}

当然,您必须在每次更改后重新加载 Nginx 配置。以下是一些有用的命令:

检查配置是否有错误:

sudo service nginx configtest

重新加载配置(这足以进行更改"work"):

sudo service nginx reload

重启整个网络服务器:

sudo service nginx restart

重要提示:

所有 server 部分必须在 http 部分内(或在 http 部分中包含的文件中):

http {
    # some directives ...
    server {
        # ...
    }
    server {
        # ...
    }
    # ...
}

下面的解决方案看起来简单明了,一切都在一个服务器块中定义。因此,通过此设置,我将所有内容强制设置为 https://www.domain.tld,因此这两个处理程序在 HTTPS 上都是非 HTTPS 和非 WWW。 有两个 IF,但如果您不想将整个 SSL 块复制两次来处理它...这就是方法。

server {
    listen 80;
    listen 443 ssl;

    server_name domain.tld www.domain.tld;

    # global HTTP handler
    if ($scheme = http) {
        return 301 https://www.domain.tld$request_uri;
    }

    # global non-WWW HTTPS handler
    if ($http_host = domain.tld){
        return 303 https://www.domain.tld$request_uri;
    }
}

避免 IF 的更好解决方案:

# Redirect all traffic from HTTP to HTTPS
server {
    listen 80;

    server_name example.com www.example.com;

    # Destination redirect base URI
    set $RURI https://www.example.com;

    location / {return 301 $RURI$request_uri;}
}

# Redirect non-WWW HTTPS traffic to WWW HTTPS
server {
    listen 443 ssl;
    # NOTE: SSL configuration is defined elsewhere
    server_name example.com;
    return 301 $scheme://www.$host$request_uri;
}

# MAIN SERVER BLOCK
server {
    listen 443 ssl;
    # NOTE: SSL configuration is defined elsewhere
    server_name www.example.com;
}

如果您有启用站点的目录,请不要使用 "http" top 指令。只需在具有以下内容的启用站点的目录中创建另一个文件(任意名称):

server {
    listen      80;   #listen for all the HTTP requests
    server_name example.com www.example.com;
    return      301         https://www.example.com$request_uri;
}

并注释掉行

listen 80; 

其中 server_name 在服务于 www.example.com

的另一个文件中相同

我搜索了很多,最后这是我的正确答案。 还记得在您的域注册商的 dns 控制面板中添加 www A 记录。

 # Force all users to https://www.example.com
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://www.example.com$request_uri;
}
server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/nginx/ssl/www.example.com.pem;
    ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
    return 301 https://www.example.com$request_uri;
}
server {
    listen 443 ssl;
    server_name www.example.com;
    root /var/www/html

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;
    ssl_certificate /etc/nginx/ssl/www.example.com.pem;
    ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
}