Nginx subfolder response inconsistent - nginx

I have a front-end application (in reactjs) and a static blog that I want to display from the same server through nginx. My application is accessible at the root of the domain / and my blog is currently accessible on a subdomain blog.domain.com. But I'm currently switching from a subdomain to the /blog subfolder on the same domain and I have an issue while displaying the blog from the subfolder. The problem only appears on chrome and is quite inconsistent. What happens is that when browsing /blog I get a 404 from the js application, not the static site (the css styles are different so it's easy to spot who responded). If I refresh the page without cache (ctrl + shift + R) I get a response from static blog. If I click on a link, again I get a 404 from the js page and if I clean-refresh now I get the correct page from the blog. Any f5 on a page redirect to a 404 from the js page.
In firefox or private navigation, everything seems fine though so I was thinking about a common cache issue in the browser at first but the fact that after dropping the cache I still get 404s from the wrong directory makes me think about some parameters that would be sent and not treated the same way in one case or another.
I have noticed that ctrl-shift-R redirects me to /blog/ from /blog when the blog is displayed properly but an f5 on /blog/ still gives a 404 from the js frontend. It might mean that the 301 to the trailing slash does something more. But a ctrl-shift-R on /blog/ does not trigger a 301 (just a 200) with the expected website displayed.
Here is my nginx config file for this site:
server {
listen 443 http2 default_server;
listen [::]:443 http2 default_server;
root /opt/react_folder/build;
index index.html;
server_name domain.com www.domain.com;
location / {
try_files $uri $uri/ /index.html =404;
}
location /blog {
alias /opt/blog_subfolder;
try_files $uri $uri/ /index.html =404;
}
... backend block
... ssl block
}
server {
listen 0.0.0.0:80;
server_name domain.com www.domain.com;
rewrite ^ https://$host$request_uri? permanent;
}

Related

Nginx - homepage redirects to /index

I am trying to configure nginx to serve pure static html pages. I am using Ubuntu 18.04.
The issue is as follows:
When trying to enter my website by url: http://www.mywebsite.com/ it changes URL automatically to http://www.mywebsite.com/index .
When using http://www.mywebsite.com/index.html it also changes URL to http://www.mywebsite.com/index - but I assume it's because of the rewrite rule which removes the .html extension from uri.
I would like to remove the silly "/index" ending when using both "/" and "index.html". I have found a solution, but not sure though if it's a "proper" one:
# If URI equals '/' then find index.html static page
location = / {
try_files /index.html $uri =404;
}
# After rewrite homepage URI equals '/index', it rewrites it to '/'
location = /index {
rewrite /index / permanent;
}
I have attempted to configure it by using simple try_files without any rewrites, returns etc. But still it always changed URI to "/index" no matter what, for the homepage.
My configs:
sites-available/mywebsite.com content
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# remove the .html extension from request URI
rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
root /var/www/mywebsite.com;
index index.html;
# redirect rules for homepage '/', removes 'index' from URI
include /etc/nginx/sites-available/conf/redirects-homepage-template.conf;
# rule for any URI content. Searches for static file with .html extension or exact URI content file name. If not found, returns 404.
location / {
try_files $uri.html $uri =404;
}
}
redirects-homepage-template.conf content
# Rules for handling homepage redirects
# If URI equals '/' then find index.html static page
location = / {
try_files /index.html $uri =404;
}
# After rewrite homepage URI equals '/index', it rewrites it to '/'
location = /index {
rewrite /index / permanent;
}
The website is meant to serve only static content and it's suppose to work as fast as possible. I wonder about the consequences of my solution and if there's any better way of removing the silly "/index" from homepage.
Haven't configured domain yet, using bare ip address for testing purpose.

Redirect nginx /index.html to root causing infinite redirects

I want to avoid having the same HTML page accessible on www.example.com and www.example.com/index.html, i.e i want to redirect the index.html to root.
This:
location = /index.html {
return 301 $scheme://www.example.com;
}
is causing me to get ERR_TOO_MANY_REDIRECTS, a redirect loop.
Any ideas what can i change to make it work?
PS this is my entire nginx conf
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include /etc/nginx/snippets/ssl-example.com.conf;
include /etc/nginx/snippets/ssl-params.conf;
include /etc/nginx/snippets/letsencrypt-challenge.conf;
root /var/www/newsite;
index index.php index.html index.htm;
server_name www.example.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location = /index.html {
return 301 $scheme://www.example.com;
}
}
This is not a detailed treatment of the subject but a simplified explanation just to answer your dilemma. The answer is that you need to abandon the attempt to do what you are doing.
Webservers can only serve specific files such as an xyz.html file. They cannot serve folders.
A call to https://www.example.com/abc/index.html is a request for the index.html file in the abc folder of the web root. A call to https://www.example.com/abc on the other hand is a request for the abc folder of the web root, which as mentioned, cannot be served.
As you have noticed however, the second call results in https://www.example.com/abc/index.html being served. This is because webservers are generally set up such that when a call is made to a folder without specifying a specific file to serve, a redirect to the index.html file in that folder is generated and this is served instead. That is, the webserver internally turns the request for https://www.example.com/abc into a request for https://www.example.com/abc/index.html.
This is what the index index.php index.html index.htm; line in your config does. It says "if there is a request for a folder without a file specified, serve the index.php file instead. If there is not such file, serve the index.html. If there is not such file, serve the index.htm file. If this also does not exist, throw a fit"
The problem is that you then go on to instruct your webserver to redirect requests for https://www.example.com/index.html to https://www.example.com which the webserver redirects back to https://www.example.com/index.html which is again redirected back to https://www.example.com in an endless loop until the webserver or your browser finally gives up.
You stated that I want to avoid having the same HTML page accessible on www.example.com and www.example.com/index.html, i.e i want to redirect the index.html to root. The question is why? There is absolutely no benefit in doing this and as you found out, you end up in a redirect loop.
You may be trying some SEO stuff but this does not apply here.

How to host 2 virtual hosts on nginx on www subdomains?

I am new to all this and trying hard already for 4 days without any big success. Maybe it's just a small thing and hopefully you can help me! Was not able to find an answer here so far (at least nothing worked out until now):
I have a domain, say example.com registered. I also added a subdomain test.example.com and added it as a CNAME to example.com.
Then I installed nginx and set it up following th tutorial on https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts with the minor change that I have both servers in one config file by now. In short, it looks like this:
in etc/nginx/sites-enabled/example.com
server {
listen 80;
root /var/www/example.com/html;
index index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
root /var/www/test.example.com/html;
index index.html index.htm;
server_name test.example.com www.test.example.com;
location / {
try_files $uri $uri/ =404;
}
}
example.com works fine and shows me "welcome to example.com" in broswer tab and some text that example.com server is running as expected (defined index.html by me as in the example).
BUT: If I go to test.example.com it shows me in the tab "welcome to example.com" either although it should be "welcome to **test.**example.com" and there is no html page at all shown to me. it's just a blank page.
Can someone help me? I don't know, if I already got the stuff with the cname domain or whether there might be something wrong.
Thank you very muh in advance!:-)
I was able to solve my problem. It was not on the server but a wrong setting in my domain provider. I don't have to forward my domain but just set it in the nameservers instead pointing to the ip!

Handle index.php and non-www in nginx

I have a nginx config file that currently handles rewriting of requests via index.php for a Zend Framework application. Example code as follows:
server {
listen 80 default_server
etc....
rewrite ^/index\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^/(.*)$ /index.php/$1 last;
}
etc....
}
However, I now need to add a redirect so that any non-www requests for any domain are processed with a 301 to the www equivalent.
I've come across examples such as:
server {
listen 80;
server_name ~^(?!www\.)(?<domain>.+)$;
return 301 $scheme://www.$domain$request_uri;
}
But how would I combine everything so that any non-www are first redirected with a 301 and then rewritten via index.php?
Two separate server blocks (exactly as you have in your question). The first server block is the default, which handles the www prefix domains already, and (presumably) handles the index.php rewrite correctly.
The second server block only matches domain names without the www prefix.
If you wanted to do it the other way around, that is rewrite index.php before you redirect, then just add a rewrite line to the new server block.

Redirect urls in nginx

I have a problem.
My app server is nginx, on which my blog was hosted.
when i visited my sitemap with this url:
http://www.ikbear.com/sitemap.xml, it works.But when i visited my sitemap with this url:
http://ikbear.com/sitemap.xml, it doesn't work. So i want to redirect http://ikbear.com/sitemap.xml to http://www.ikbear.com/sitemap.xml, would you tell me how can i do that in nginx? Thanks!
Actually I'm going to venture a guess that you'll have the same trouble redirecting that url as actually serving it.
First, here's the syntax for a basic redirect:
server {
# ...
# redirect sitemap.xml to sitemap.xml.php
rewrite ^(/sitemap.xml)$ /sitemap.xml.php;
# ...
}
What might work for you is getting both www and not-www serving correctly. A common strategy is to serve all www to non-www, or vice versa. Here's an example of that:
server {
listen 80;
server_name www.mydomain.com;
# forward everything from www.domain.com to domain.com
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
listen 80;
server_name domain.com *.domain.com;
location / {
root /var/www/domain/htdocs;
index index.html index.htm index.php;
# ... etc.
}
}

Resources