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/;
}
Related
I have two react applications, one at '/home/user/www' and one at '/home/user/builds/checkout'. I want any url starting with '/checkout' eg. '/checkout/complete', '/checkout/error' to use that application. I have the below setup in my Nginx config file:
root /home/user/www;
index index.html index.htm;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
location ~ ^/checkout?(.*)$ {
allow all;
root /home/user/builds;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
try_files $uri $uri/index.html =404;
}
location ~ ^.+\..+$ {
try_files $uri =404;
}
going to the url '/checkout' is working correctly but any other url begining with '/checkout' like '/checkout/complete' and 'checkout/error' are just returning a 404 page.
Your configuration looks too complicated. Can you try this one?
root /home/user/www;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
location /checkout/ {
root /home/user/builds;
try_files $uri $uri/ /checkout/index.html;
}
If the /checkout URI won't redirect you to /checkout/, add this:
location = /checkout {
return 301 /checkout/;
}
Can anyone tell me why is this ngnix config doesn't match all URL that starts with /admin :
location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
It always fall back to the default content of location / . However, I hardcoded all the possible URL in the Nginx config, it works and only matches the hard coded URL, something like :
location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
location /admin/news/ {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
location /admin/another-url/ {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
Thanks for your help.
The final term of the try_files statement is a URI. The URI of the index.html file at /path/to/admin/build/index.html is /admin/index.html.
Using alias and try_files in the same location block can be problematic.
You may want to use a more reliable solution:
location ^~ /admin {
alias /path/to/admin/build;
if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}
The location and alias values should both end with / or neither end with /. The ^~ operator will prevent other regular expression location blocks from matching any URI that begins with /admin. See this caution on the use of if.
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.
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;
}
It's possible in nginx to do something like:
location /backend {
try_files $uri #proxy_to_app;
}
location /{
try_files $uri #proxy_to_frontend;
}
It works when I go to /backend but if I go to /backend/something_more I'm redirected to /something_more