DEV Community

Brandon Michael Hunter
Brandon Michael Hunter

Posted on

Problem: nginx problems redirecting non-www to my index.html page

Hey reader,
Today I wanted to share with you an issue I recently resolved regarding configuring server blocks on a nginx web server.

Problem:

I wanted all of my non-www application url requests browsing to my custom index.html page. So I used the following server blocks configuration:

server { root /var/www/domain_name.docker.hub.registry/html; index index.html; listen 80; listen 443 ssl; server_name domain_name.docker.hub.registry domain_name.docker.hub.registry.domain_nameetc.com; location / { try_files $uri $uri/ =404; } #listen 443 ssl; ssl_certificate /etc/letsencrypt/live/domain_name.docker.hub.registry.domain_nameetc.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/domain_name.docker.hub.registry.domain_nameetc.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot return 301 https://www.domain_name.docker.hub.registry.domain_nameetc.com$request_uri; } server { root /var/www/domain_name.docker.hub.registry/html; index index.html index.htm; # listen to the standard http port 80 #listen 80; listen 443 ssl; server_name domain_name.docker.hub.registry www.domain_name.docker.hub.registry.domain_nameetc.com; location / { try_files $uri $uri/ =404; } ssl_certificate /etc/letsencrypt/live/www.domain_name.docker.hub.registry.domain_nameetc.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/www.domain_name.docker.hub.registry.domain_nameetc.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } 
Enter fullscreen mode Exit fullscreen mode

As a result using the specific application urls and current server block configuration, produced the following results:

Solution:

My solution was to create a server block for each application url. See below

server { root /var/www/<domain>.docker.hub.registry.<domain>.com/html; index index.html; server_name <domain>.docker.hub.registry.<domain>.com; listen 80; listen [::]:80; location / { try_files $uri $uri/ =404; } } server { root /var/www/<domain>.docker.hub.registry.<domain>.com/html; index index.html; server_name www.<domain>.docker.hub.registry.<domain>com; listen 80; listen [::]:80; location / { try_files $uri $uri/ =404; } } server { root /var/www/<domain>.docker.hub.registry.<domain>.com/html; index index.html; server_name <domain>.docker.hub.registry.<domain>.com; listen 443 ssl; listen [::]:443 ssl; ssl_certificate /etc/letsencrypt/live/<domain>.docker.hub.registry.<domain>.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/<domain>.docker.hub.registry.<domain>.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot location / { try_files $uri $uri/ =404; } } server { root /var/www/<domain>.docker.hub.registry.<domain>.com/html; index index.html; server_name www.<domain>.docker.hub.registry.<domain>.com; listen 443 ssl; listen [::]:443 ssl; ssl_certificate /etc/letsencrypt/live/<domain>.docker.hub.registry.<domain>.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/<domain>.docker.hub.registry.<domain>.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot location / { try_files $uri $uri/ =404; } } 
Enter fullscreen mode Exit fullscreen mode

Now we have all application urls now browsing to the index.html page that I wanted.

That's it. Since I'm new to nginx and configuring server blocks, please provide your feedback with any improvements.

The original question was posted on stackoverflow.

Thanks for reading :)

Top comments (0)