I need my Nginx setup to redirect users to www.example.com if they type example.com in the browser. The reason is that our SEO consultant said there should be only one preferred domain, otherwise Google sees it as content duplication. Anyway . . .
So the point is, I also have SSL from Letsencrypt set up on the server, but I'm not able to achieve the redirect from example.com to www.example.com (the server accepts both the versions). Here's the configuration I'm using:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com www.example.com;
root /home/my_site;
index index.php index.html index.htm;
# for letsencrypt
location ~ /.well-known {
allow all;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
==== Update ====
I now changed my configuration to as advised by Tim (and I always to nginx -t
and restart
) in one of the answers to the following:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
root /home/ankush/wp_ankushthakur;
index index.php index.html index.htm;
# for letsencrypt
location ~ /.well-known {
allow all;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Here's the output of curl -k
and access logs for all of the variations (I didn't build Nginx from source because I'm hoping for a simpler solution and don't want to mess up the server):
curl -k http://example.com
Curl output: 301 - Moved permanently
Access logs: "GET / HTTP/1.1" 301 194 "-" "curl/7.47.0"
curl -k http://www.example.com
Curl output: 301 - Moved permanently
Access logs: "GET / HTTP/1.1" 301 194 "-" "curl/7.47.0"
curl -k https://example.com
Curl output: 301 - Moved permanently
Access logs: "GET / HTTP/1.1" 301 194 "-" "curl/7.47.0"
curl -k https://www.example.com
Curl output:
Access logs: "GET / HTTP/1.1" 301 5 "-" "curl/7.47.0"
Notice the last section, where the CURL output is blank and the access logs still give a permanent redirect.
Funnily enough, if I comment out the second server
block and then restart Nginx, I end up with the opposite effect of what I wanted: www redirects to non-www! I'm surprised that's happening, because the HTTPS version of www.example.com isn't mentioned anywhere in this (third) version of the config.
Answer
I was finally able to convince our SEO person to consider the non-www domain as primary. The configuration that worked to redirect www to non-www was as below. Although my attempt for achieving the reverse had a similar configuration, I'm not sure what was preventing it.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com;
root /home/mysite;
index index.php;
location ~ /.well-known {
allow all;
}
location / {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri $uri/ /index.php?$query_string;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
No comments:
Post a Comment