极
极核实验室
Nginx: Force HTTPS Redirect and Add www

Nginx: Force HTTPS Redirect and Add www

AI summary
文章介绍了如何通过修改Nginx配置文件实现网站强制跳转HTTPS和添加WWW域名,包括使用rewrite方法强制HTTP到HTTPS的跳转和利用301重定向将非WWW域名跳转到带WWW的HTTPS域名,并提供了具体的配置代码和修改步骤,强调修改后需重启Nginx Server使设置生效。

Repost license

Request authorization before reposting and retain the authorization code, original URL, and go/affiliate links.

Original URL:https://www.geekcorelab.com/post/nginxs-mandatory-redirect-to-https-and-add-to-www

The commercial link is an authorized go short link that must stay on the reposted page. It records the source before sending readers to the original article.

Redirect to HTTPS

Although many websites support https, when you directly enter the URL in the browser address bar, they are accessed via the http protocol by default, hence the need to force http to redirect to https arises
Use the rewrite method to force HTTPS

if ($server_port !~ 443){
    rewrite ^(/.*)$ https://$host$1 permanent;
}

Modification method
Enter the virtual host configuration file directory
Enter the configuration file directory

View the virtual host to modify
View the virtual host to modify

Use the vim editor to modify the configuration file and add the above code
Just add the code

After modification, you need to restart Nginx Server for it to take effect.

Redirect to WWW

For some websites, the main domain has www added, but you can still access gqink.cn if you want to access it; then add the following code to redirect to www.biliwind.com
Use a 301 redirect to HTTPS and add www

if ($host ~ '^gqink.cn'){
    return 301 https://www.biliwind.com$request_uri;
}

Modification method
Enter the virtual host configuration file directory
Enter the configuration file directory

View the virtual host to modify
View the virtual host to modify

Use the vim editor to modify the configuration file and add the above code
Just add the code

After modification, you also need to restart Nginx Server for it to take effect.
This article uses the lnmpa environment for demonstration.

Comments

Discussion

Be kind and keep the discussion friendly.

Complete your profile details to improve the chance of approval.

Your email will not be shown publicly.

Loading...

Markdown supported.

Discussion

0 comments

Internal link graph

Loading internal links…

View linked articles