Now more and more websites are enabling https access. SSL certificates are not expensive, and there are some free ones such as Let's Encrypt or Tencent also provide 1-year free certificates, etc., plus https has better security, and some people even say that search engines will prefer https websites in the future... Today I will record several ways to force HTTP to jump to HTTPS, so that I can add SSL one day. Use.
We need to modify the domain name configuration file. Here I take Junge LNMP environment as an example. The path is /usr/local/nginx/conf/vhost/
http_https
As shown in the picture above, the selection part is the added jump content. Usually we have multiple writing methods, which are listed below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
No.1
if ($scheme = http ) {
return 301 https://$host$request_uri; }
 
No.2
server_name vpsok.net ;
rewrite ^(.*) https://www.vpsok.net$1 permanent;
 
No.3
if ($server_port = 80) {
return 301 https: //$host$request_uri; }
 
No.4
server_name vpsok.net ;
return 301 https://$server_name$request_uri;

Extension:
If the website does not use NGINX but APACHE, it is even simpler. Modify the .htaccess file directly in the root directory of the website and add:

1
2
3
4
5
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.vpsok.net$ [NC]
RewriteRule ^(.*)$ https://www.vpsok.net/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.vpsok.net/$1 [L,R=301]

In this case, in addition to domain name access without adding www, it will also automatically jump to the www domain name and access via HTTPS.
Other ways to write NGINX:

1
2
3
4
5
6
7
8
rewrite ^(.*)$? https://$host$1 permanent; #1
return 301 https://$server_name$request_uri; #2
if ($host ~* "^vpsok.net$ ") {
rewrite ^/(.*)$ https://www.vpsok.net/ permanent;
} #3
if ($host = www.vpsok.net) {
rewrite ^/(.*)$ http://www.vpsok.net permanent;
} #4

The above is collected from the Internet. If there are any errors or omissions, please help me correct them! Thanks.

postid
5156

Leave a Reply