nginx match location after prefix - nginx

The problem is that nginx is matching paths correctly on www.example.com/en/
or www.example.com/pl/ but not www.example.com/en/something/. If I go to www.example.com/en/something/ then I get "Welcome to nginx!" page.
When I visit www.example.com/en/ then do action that redirects to www.example.com/en/something/ - this scenario works.
I tried locations: '/en', '^~ /en'.
What's going on?
my nginx.conf is looking like this:
server {
index index.html index.htm index.nginx-debian.html;
server_name xxx; # managed by Certbot
location / {
root /usr/share/nginx/html/en;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /en/ {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /pl/ {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}

With your current configuration, you use try_files $uri $uri/ /index.html =404;.
Ignoring the redundant =404 on the end, if the file is not found, the file located at /usr/share/nginx/html/index.html will be returned. And that file probably contains "Welcome to nginx!".
All parameters of the try_files directive are like URIs, and the correct URI for the /en/ index page is /en/index.html.
For example:
index index.html index.htm;
root /usr/share/nginx/html;
location /en/ {
try_files $uri $uri/ /en/index.html;
}
location /pl/ {
try_files $uri $uri/ /pl/index.html;
}
See this document for details.

Related

Getting 500 internal error on redirect rule of alias in nginx

I am newbie to nginx server. I am getting stuck in URL redirection. I have following lines to default file.
server {
listen 80;
root /home/ubuntu/web/server/current/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
error_log /home/ubuntu/web/error.log info;
location / {
# try_files $uri $uri/ /index.php?$query_string;
# try_files $uri $uri/ =404;
}
rewrite ^/web/(.*) /web/;
location /web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
what I expect from above rewrite rule is - all URLs like - http://example.com/web/login, http://exmpale.com/web/dashboard will be redirected to /home/ubuntu/web/client/web/ and the default page to hit is index.html file.
When I open the error log file then i found error like -
rewrite or internal redirection cycle while internally redirecting to "/web/index.php", client: ip_address, server: _, request: "GET /web/ HTTP/1.1", host: "ipaddress"
What i am doing wrong here.
Answer credit goes to #RichardSmith, he provided possible error which is in right direction. I figure out my mistake in nginx rewriting rule.
rewrite ^/web/(.*) /web/;
location /web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
Instead of above I should have following-
rewrite ^/web/(.*) web/;
location web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
Server consider path as absolute whenever / placed before web. Thus rewrite statement tries to redirect to fallback file which is not existed in absolute path. Eventually, rewrite statement makes never ending loop.

try_files directive multiple options not working

I have the following code:
server {
.....
root /user/share/nginx/html;
rewrite ^(/.*)\.html(\?.*)?$ $1$2 redirect;
location / {
try_files $uri/index.html $uri.html $uri index.html =404;
}
In the rewrite I remove the .html extension from file
In location I uri corresponding file is not found I offer a set of other options.
The last one before the error code is index.html; index.html exist but always I get the 404;
All Nginx URIs begin with a leading /. Use: /index.html.
For example:
location / {
try_files $uri/index.html $uri.html $uri /index.html;
}

Nginx regexp all paths expect

I have a 3 type of paths /api, /,/some_path_here
If user have requested page with empty path (/) I want to redirect to login .
So I have a config file look like this
location /api {
try_files $uri $uri/ /index.php?$args;
}
location /[0-9a-z] {
try_files $uri $uri/ /index.html;
root /var/www/cabinet/client/dist;
}
location / {
return 301 https://my_domain.com/login;
}
But when trying to request https://my_domain.com/ its redirect to https://my_domain.com/login with error ERR_TOO_MANY_REDIRECTS
How can I solve this error?
Here is a solution
location /api {
try_files $uri $uri/ /index.php?$args;
}
location / {
try_files $uri $uri/ /index.html;
root /var/www/cabinet/client/dist;
}
location = / {
return 301 $scheme://$server_name/login/;
}

Nginx configuration for single page app and nested directories

I have a directory/files structure such as:
root/
a/
utils.js
b/
assets/
styles.css
app.js
index.html
And I want to configure nginx to serve files from a directory directly if exist and have single page app in directory b (if file in path exists the it wil be served directly, nd if not the fallback will end up at index.htm file.
For example:
myapp.com/a/utils.js will return that file.
myapp.com/b/ or myapp.com/b/foo will display index.html
myapp.com/b/assets/style.css will return directly css file
I tries multiple different configurations and non had worke so far. For exampe the simplest:
server {
listen 80;
root /root;
index index.html;
location / {
try_files $uri $uri/ /index.html =404;
}
}
I also tries something to serve different directories:
server {
listen 80;
root /root;
index index.html;
location /a {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /index.html =404;
}
}
I tried to define different roots as well:
server {
listen 80;
index index.html;
location /a {
root /root/a;
try_files $uri $uri/ =404;
}
location /b {
root /root/b;
try_files $uri $uri/ /index.html =404;
}
}
Nginx seems to ignore existing files and ends up returning 404 page at all times. When I try to access soe existing file directly it gets redirected to / (root) url regardless.
The last parameter of a try_files statement is the default action. There can only be one. Many of your examples have two. See this document for details.
The correct URI for your index.html file is /b/index.html which is what you need to use for the default action of the try_files statement.
This should meet your requirements:
location / {
try_files $uri $uri/ /b/index.html;
}
You do not state what should happen with the URI /a/foo. In the above case, it would also return index.html. If you need it to return a 404 response, you would use:
location / {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /b/index.html;
}
See this document for more.

Nginx: Unable to run multiple site in single URL

In My application i have 3 sites - User frontend , Admin panel and Splash page .I want to run User frontend in localhost:8080 , Admin panel in localhost:8080/admin and splash in localhost:8080/splash
To run this all i used the following configuration:
server {
listen 8080;
server_name localhost;
root /home/ajit/git/univisior;
location / {
alias /home/ajit/git/univisior/FrontEnd/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /admin{
alias /home/ajit/git/univisior/admin/dist/;
index index.html;
try_files $uri $uri/ index.html;
}
location /splash {
alias /home/ajit/git/univisior/Splash/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:3000/api;
}
}
The issue with this configuration is this
When i am opening admin site (localhost:8080/admin) i am getting error
VM189:1 Uncaught SyntaxError: Unexpected token <
but when i am running admin site with single site configuration it works fine , nginx config is pasted below:
server {
listen 9010;
server_name localhost;
root /home/ajit/git/univisior/admin/dist;
index index.html index.htm;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:3000/api;
}
location /static {
alias /opt/univisor;
}
}
With splash page i am unable to get images.splash also works fine if i am running this with single site configuration just like admin.
User front is working fine but i am facing issue with admin and splash.can anybody help me out of this problem.
Thanks
Remove the trailing / from your alias directives:
location / {
alias /home/ajit/git/univisior/FrontEnd/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /admin{
# alias /home/ajit/git/univisior/admin/dist/;
alias /home/ajit/git/univisior/admin/dist;
# index index.html;
# try_files $uri $uri/ index.html;
try_files $url $uri/;
}
location /splash {
# alias /home/ajit/git/univisior/Splash/dist/;
alias /home/ajit/git/univisior/Splash/dist;
# index index.html;
# try_files $uri $uri/ /index.html;
try_files $uri $uri/;
}
I was able to figure out the issue. The issue in both admin and splash page were same. The path for images and scripts files in index.html was written as
<img src="../images/logo.png" alt="logo">
.
.
<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../bower_components/angular/angular.js"></script>
When running index file with single site nginx configuration (admin site configuration pasted above in question) both admin and splash works fine. But when running both of them in configuration like this
.
.
location / {
alias /home/ajit/git/univisior/FrontEnd/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /admin{
alias /home/ajit/git/univisior/admin/dist/;
index index.html;
try_files $uri $uri/ index.html;
}
location /splash {
alias /home/ajit/git/univisior/Splash/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
.
.
makes issue as <img src="../images/logo.png" alt="logo"> will look for url localhost:8080/images/logo.png instead of localhost:8080/admin/images/logo.png same applies for scripts because of this browser is not able to get scripts/images.
Inorder to solve this i replaced ../ from index.html to have a final index.html like this.
<img src="images/logo.png" alt="logo">
.
.
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>

Resources