Current location: home page > Industry trends >Text content

Setting method of forced HTTPS jump in Apache / nginx server environment

Dazzle Yi Three years ago (2017-09-21) Industry trends one thousand and twenty-six

Although we have seen all kinds of news or so-called authoritative suggestions, websites are required to enable the HTTPS encryption format, but many large websites have not been started. Of course, the form of HTTPS encryption has advantages, but it can not force all websites to be enabled. For example, simple personal website, in fact, there is no need to use, but it will increase some costs and technical difficulties.


In the general web server environment, Apache or nginx environment driver will be adopted, and several scripts can be used to enforce the encryption of HTTP address. Of course, the premise is that we have deployed SSL certificates in the server.


First, Apache enforces the form of HTTPS


1. Add file location


Generally, it is a pseudo static file in the root directory of the website. If not, we need to create a. Htaccess file.


2. Force www jump

 RewriteEngine On
RewriteCond %{HTTP_ HOST} !^www.wanbk.net$ [NC]
RewriteRule ^(.*)$ http://www.wanbk.net/$1 [L,R=301]
RewriteCond %{SERVER_ PORT} 80
RewriteRule ^(.*)$ http://www.wanbk.net/$1 [L,R=301]

3. Force non www jump

 RewriteEngine On
RewriteCond %{HTTP_ HOST} !^wanbk.net$ [NC]
RewriteRule ^(.*)$ https://wanbk.net/$1 [L,R=301]
RewriteCond %{SERVER_ PORT} 80
RewriteRule ^(.*)$ https://wanbk.net/$1 [L,R=301]

Add it to the root directory. Htaccess file according to whether to use WWW or not, and take effect immediately.


Second, nginx forces HTTPS jump


1. Set file location


Generally, the. Conf file of the corresponding website domain name in the current server environment is added to the configuration environment.


2. Force www jump

 if ($ssl_ protocol = "") { return 301 https://$host$request_ uri; }
if ($host != www.wanbk.net) { return 301 $scheme://www.wanbk.net$request_ uri; }

3. Force non www jump

 if ($ssl_ protocol = "") { return 301 https://$host$request_ uri; }
if ($host != wanbk.net) { return 301 $scheme://wanbk.net$request_ uri; }

After setting, you need to restart nginx to take effect.

Comment list

 wxz
Three years ago (2017-09-22)

Yes, for my website, HTTPS is a burden on the server and on me

Dazzle Yi reply:
After all, it's just a personal blog. HTTPS is a trend
Three years ago (2017-09-22)

Comment

 visitor

Welcome to participate in the discussion. Please express your views and exchange your views here.
Call for istwar