nginx Strip www and only go to example.com
I've been working for hours trying to get the DNS/Nginx setup correct to direct ALL traffic to
http://khepri.voyage and want the 'www' in http://www.khepri.voyage to not show.
My khepri.voyage.conf:
server (
listen 80;
server_name khepri.voyage www.khepri.voyage;
root /var/www/khepri.voyage
index index.php;location / {
try_files $uri $uri/ /index.php$args;
}location ~* .php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
I've tried almost everything I could find online:
new server block stripping 'www' and 301 redirect; that's a constant fail; and two server.conf files, one with the redirect specifically.
I was able to use a DNS lookup and arrive at the correct IP address for both khepri.voyage and www.khepri.voyage. So, I know that the 'A' names work.
I would like to strip the 'www' and have it only show 'khepri.voyage'.
I followed the guide on Linode specifically for Debian 9 and have looked at many, many other websites for the last 7 hours trying to get this to work. Any help would be highly appreciated. Thank you.
1 Reply
What you want is two server{ } blocks in your nginx config:
1 - which matches on your desired server name (without the www), this will be the "real" thing with directives for serving content (location blocks, root, fast_cgi, etc.)
server {
listen …;
server_name site.com;
root /var/www/html;
…..
}
2 - which matches on unwanted server names and redirects to the first one
server {
listen …;
server_name www.site.com;
rewrite ^ $scheme://site.com$uri last;
}