本文介绍了强制www.和nginx.conf(SSL)中的https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

购买SSL证书后,我一直试图将所有页面强制设置为安全的https和www.

After purchasing a SSL certificate I have been trying to force all pages to secured https and to www.

https://www.exampl.com 可以正常使用,但必须正确输入. www.example.com或example.com仍指向http.

https://www.exampl.com is working and secure but only if type it in exactly. www.example.com or example.com are still pointing to http.

我们使用nginx作为代理,并且需要在此处输入重写.我可以通过Putty进行SSH/root访问.我已经通过输入腻子来访问nginx.conf.

We use nginx as a proxy and need to input the rewrite there. I have SSH / root access via Putty. I have accessed nginx.conf by inputting into putty.

现在呢?我要在此页面上输入nginx命令吗?光标从哪里开始?首先有命令行吗?

HTTPS:

.htacess – 在我不得不输入nginx之前输入了原始代码

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

Nginx代码转换器– 这是它在转换器上的显示方式.一切都正确吗?

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

然后

万维网

.htacess – 在我不得不输入nginx之前输入了原始代码

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

Nginx代码转换器– 这是它在转换器上的显示方式.一切都正确吗?

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

}

然后保存吗?重新开始?

Do I then save? Restart?

任何帮助将不胜感激.我已经为此战斗了好几个星期.我的托管公司尽可能地提供了帮助,现在我正在即时学习中…….还是我应该停下来并雇用一名开发商? $$$

Any help would be greatly appreciated. I have been battling this for weeks. My Hosting company helped as far as they could, now I am learning on the fly…. Or should I just stop and hire a developer? $$$

谢谢

推荐答案

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

The best way to implement WWW and HTTPS redirection is to create a new server section in Nginx config:

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 https://www.example.com 重定向.可以使用类似于以下代码的代码来完成此操作:

You will also have to perform https://example.com to https://www.example.com redirection. This may be done with code similar to the following:

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配置.以下是一些有用的命令:

And of course, you must reload Nginx config after each change. Here are some useful commands:

检查配置中的错误:

sudo service nginx configtest

重新加载配置(这足以使更改生效"):

sudo service nginx reload

重新启动整个网络服务器:

sudo service nginx restart

重要说明:

Important note:

您所有的server部分都必须位于http部分中(或位于http部分中包含的文件中):

All your server sections must be inside http section (or in a file included in http section):

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

这篇关于强制www.和nginx.conf(SSL)中的https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:53