From the official document of Nginx documentation, the correct nginx https 301 jumps to the method with the WWW domain name as follows:
HTTP 301 jump to the With WWW domain name method
Copy Code code as follows:
server {
Listen 80;
server_name example.org;
return to Http://www.example.org$request_uri;
}
server {
Listen 80;
server_name www.example.org;
...
}
HTTPS 301 jump to with www domain name method
Copy Code code as follows:
server {
Listen 80;
server_name www.domain.com;
$scheme'll get the HTTP protocol
and are best practice for tablet, phone, desktop and SEO
return $scheme://domain.com$request_uri;
}
server {
Listen 80;
server_name domain.com;
Here goes the rest of your config file
Example
Location/{
Rewrite ^/cp/login?$/cp/login.php last;
etc etc ...
}
}
Use the NGINX-V command to check the version of the nginx you are talking about. The following is for the older version of nginx301 jump to the with WWW domain name method from Www.ksharpdabu.info to Ksharpdabu.info
Copy Code code as follows:
server {
server_name www.domain.com;
Rewrite ^ (. *) http://domain.com$1 permanent;
}
server {
server_name domain.com;
#The Rest of your configuration goes here#
}
So two server segments are required.
Jump from Ksharpdabu.info to Www.ksharpdabu.info
Copy Code code as follows:
server {
server_name domain.com;
Rewrite ^ (. *) http://www.domain.com$1 permanent;
}
server {
server_name www.domain.com;
#The Rest of your configuration goes here#
}
According to the above settings, use the rewrite method to jump to the designated domain name, conducive to SEO
Here is my example, from www.google.com jump to google.com part of Nginx configuration content:
Copy Code code as follows:
server {
server_name www.google.com;
Rewrite ^ (. *) http://google.com$1 permanent;
}
server {
Listen 80;
server_name google.com;
Index index.php index.html;
####
# now pull ' site from one directory #
Root/var/www/www.google.com/web;
# done #
Location =/favicon.ico {
Log_not_found off;
Access_log off;
}
}
There is also a way of not rewirte on the Internet, as follows:
Copy Code code as follows:
server {
#listen is default
server_name www.example.com;
return $scheme://example.com$request_uri;
}
server {
#listen is default
server_name example.com;
# # Here goes the rest of your conf ...
}
Because return can be used for all versions, rewrite may cause a 301 error due to a different version. And you can stop the execution of matching and searching directly.
The following include HTTP and HTTPS. The same server.
Copy Code code as follows:
server {
Listen 80;
Listen 443 SSL;
server_name www.example.com;
return $scheme://example.com$request_uri;
}
server {
Listen 80;
Listen 443 SSL;
server_name example.com;
# Rest goes here ...
}
The $scheme variable will contain only HTTP if your server listens to only 80 ports (the default is 80 ports) and does not contain SSL keywords. If you do not use this variable, you will not get the jump result you want.
Forces all HTTP to be skipped to HTTPS, SSL (personal config on UNIX with IPv4, IPV6, SPDY, ...):
Copy Code code as follows:
#
# Redirect All www to non-www
#
server {
server_name www.example.com;
Ssl_certificate SSL/EXAMPLE.COM/CRT;
Ssl_certificate_key Ssl/example.com/key;
Listen *:80;
Listen *:443 SSL spdy;
Listen [::]:80 Ipv6only=on;
Listen [::]:443 SSL Spdy Ipv6only=on;
return to Https://example.com$request_uri;
}
#
# Redirect all non-encrypted to encrypted
#
server {
server_name example.com;
Listen *:80;
Listen [::]:80;
return to Https://example.com$request_uri;
}
#
# There We go!
#
server {
server_name example.com;
Ssl_certificate SSL/EXAMPLE.COM/CRT;
Ssl_certificate_key Ssl/example.com/key;
Listen *:443 SSL spdy;
Listen [::]:443 SSL Spdy;
# Rest goes here ...
}
#
# Redirect All www to non-www
#
server {
server_name www.example.com;
Ssl_certificate SSL/EXAMPLE.COM/CRT;
Ssl_certificate_key Ssl/example.com/key;
Listen *:80;
Listen *:443 SSL spdy;
Listen [::]:80 Ipv6only=on;
Listen [::]:443 SSL Spdy Ipv6only=on;
return to Https://example.com$request_uri;
}
#
# Redirect all non-encrypted to encrypted
#
server {
server_name example.com;
Listen *:80;
Listen [::]:80;
return to Https://example.com$request_uri;
}
#
# There We go!
#
server {
server_name example.com;
Ssl_certificate SSL/EXAMPLE.COM/CRT;
Ssl_certificate_key Ssl/example.com/key;
Listen *:443 SSL spdy;
Listen [::]:443 SSL Spdy;
# Rest goes here ...
}