Site is not redirecting to backend node NGINX config - nginx

I have a react frontend with a node backend but my nginx setup is currently not working. What I want to do is redirect https://sales.example.com/api/stats/get_customer_count/2323232 to http://127.0.0.1:3000/stats/get_customer_count/2323232(Node backend server runs on http://127.0.0.1:3000) but I keep getting 404 errors stating that the api path is not found. Not sure how to go about fixing this. Appreciate it. Thanks
server {
root "/home/sales/frontend/dist";
index index.html index.htm;
server_name sales.example.com;
error_log /var/log/nginx/error.log;
listen 80;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location ~* ^/api/ {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}

This seems to be answered here already, and the explanation written by #Dayo is good.
Translated it to your example, it looks like this: (notice, the main difference is the tailing slash in the proxy pass)
location ~* ^/api/ {
proxy_pass http://127.0.0.1:3000/;
}
But please, read through the linked answer, before copying this over.

Related

Reverse proxy nginx to itself

I am currently hosting a single-page react app that is hosted in the URL root like so:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I need to put the site behind an AWS elastic load balancer and at the same time change the path so everything is within a /support directory e.g. http://example.com/index.html -> http://example.com/support/index.html.
AWS ALBs do not support URL rewriting so I have to do this within the nginx config on the server. First of all I tried changing the config to:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
This sort-of works but the URLs within the javascript content don't contain the /support path (e.g. they contain http://example.com/script.js instead of http://example.com/support/script.js).
I then tried creating a reverse-proxy config to proxy /support to /, which sadly put nginx in an infinite loop until it ran out of worker threads:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I'm confused why requests are going into a reverse-proxy loop? Shouldn't proxy_pass remove the /support prefix before proxying the request, and therefore it shouldn't be "caught" again by the /support location?
Just a guess.
Do you want to serve something on /?
If not - it is easy:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
Fiddle around with the ending slashes if it does not work (using them - or not - makes often a difference).
Use alias instead of root so that /support is not added to the /var/www/html folder.
Everything gets redirected to /support.
If you want to serve something on / which is different from /support:
Use sub_filter or subs_filter in /support to rewrite your source code links on-the-fly so that they will never use /.
If you have redirects inside your source code (or proxy_pass backend) - you need proxy_redirect and/or Lua to catch and change them on-the-fly.

Dokku redirects to another domain when requested site is down

I have Dokku installed on a server, with multiple sites/domains deployed to it. When one of my sites goes down, all HTTP requests to it get redirected (for some reason) to another site. This is confusing. I'm expecting Dokku to show some error page in this case. Is it the default behavior or I did something wrong?
PS. This is the problem: https://github.com/dokku/dokku/issues/2602
How about adding a custom error page based on the error code by editing vhost file:
server{
server_name www.foo.com;
root /srv/www/foo/public_html;
expires 1M;
access_log /srv/www/foo/logs/access.log;
error_log /srv/www/foo/logs/error.log;
error_page 404 /404.html;
location / {
index index.html;
rewrite ^/(.*)/$ /$1 permanent;
try_files "${uri}.html" $uri $uri/ =404;
}
location = /404.html {
internal;
}
}
Your server error might be caught from codes 404 or 500

How to correct my nginx configuration if it is wrong?

I am new to nginx. I try to learn using search www and stackoverflow. Mostly I get help to understand how to build my nginx configuration.
I have my domain as www.mysite.com. Everything; landing page, the error and server error must be redirect to default index.html. I also define my access and error log. All this done (below code) inside the /etc/nginx/conf.d/default.conf.
I need to redirect (proxy_pass) /admin, /user and anything related to them. Example the /admin has also different folder like /admin/login/. I need to everything after /admin must be redirected. The same goes also for the /user as well.
1) Looking at my code am I redirect the location /admin and location /user correctly?
2) I also use try_files $uri $uri/ =404; in redirection. which also redirects the 404 to default index.html. Am I doing right?
3) I am also denying access to some file and folder. Am I doing right?
My main question is How to correct my nginx configuration if it is wrong? So to understand the correct nginx configuration I divide my question to 3 different question above. I hope I didnt brake stackoverflow how to ask question guidelines.
Thank you.
UPDATE:
server {
charset UTF-8;
listen 80;
listen [::]:80;
server_name www.mysite.com;
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/host.error.log main;
# define a root location variable and assign a value
set $rootLocation /usr/share/nginx/html;
# define www.mysite.com landing page to the static index.html page
location / {
root rootLocation;
index index.html index.htm;
}
# define error page to the static index.html page
error_page 404 /index.html;
location = /index.html {
root rootLocation;
internal;
}
# redirect server error pages to the static index.html page
error_page 500 502 503 504 /index.html;
location = /index.html {
root rootLocation;
internal;
}
# redirect www.mysite.com/admin to localhost
# /admin or /admin/ or /admin/**** must be redirect to localhost
location /admin {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3000";
}
# redirect www.mysite.com/user to localhost
# /user or /user/ or /user/**** must be redirect to localhost
location /user {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3001";
}
}
It is usual to place the root statement once in the server block, rather than repeat the same value in multiple location blocks.
You are using proxy_pass to change the URI before passing it upstream. In this case, the location value and the URI part of the proxy_pass value should either both end with / or neither end with /. See this document for details.
Usually you do not want to place try_files and proxy_pass in the same location. This causes Nginx to check for the existence of the file in its document root before allowing the request to pass upstream.
You should not need to deny access to the configuration files, as these file should not be within the document root in the first place.

nginx: fallback to try_files when proxy_pass fails requires unusual config

I am using Nginx to server a single page app. Basically we just need to serve the index.html page whenever no matching file is found. The location looks like this and has been working just fine:
location / {
try_files $uri $uri/ /index.html
}
Now I would like to query an upstream server, and only if that fails, use the try_files directive as above
If the try_files is just moved to a fallback location like
location #fallback {
try_files $uri $uri/ /index.html;
}
location / {
proxy_pass http://127.0.0.1:8080;
proxy_intercept_errors on;
error_page 400 403 502 503 504 #fallback;
}
then - when the upstream server is unavailable - the client sees the Nginx 502 error page instead of the files served from the file system.
I finally found a solution that works by using a double slash in front of the /index.html fallback. This is the whole config file which can be used with the official nginx docker image for testing
events {
}
http {
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
server {
listen 80;
root /usr/share/nginx/html/;
location / {
proxy_pass http://127.0.0.1:9990;
proxy_intercept_errors on;
error_page 400 403 502 503 504 = #fallback;
}
location #fallback {
try_files $uri?$args /index.html //index.html;
}
}
}
which can be run with a command like
docker run -v /path/to/www/folder:/usr/share/nginx/html:ro -v /path/to/config/nginx.conf:/etc/nginx/nginx.conf -d -p 8080:80 nginx
In case no double slash is present before the last index.html fallback like
location #fallback {
try_files $uri?$args /index.html;
}
Then nginx constructs a path on the filesystem like <root>index.html, which has a missing delimiter instead of the correct <root>/index.html, whenever a url which is not the root url is requested.
Final question: why does this setup require a double slash within the try_files directive? Why can't one just use the try_files section from a regular config and move it to a fallback location used when intercepting errors?
I was presented with a similar situation, and I solved this going the other way around, using the suggestion from this page of common pitfalls. That is, first serve the static files, and then fallback to the proxy:
location / {
try_files $uri $uri/ #proxy;
}
location #proxy {
proxy_pass http://127.0.0.1:9990;
}
In this case, this would first look for the presence of the files as static files in the root, then proxy the request to http://127.0.0.1:9000. This is functionnally equivalent unless you want the files from the proxy to shadow the static files.

nginx Redirect subfolder to root domain

I want to redirect the subfolder and all contents to root domain.
For example:
http://www.example.com/ubb/ will redirect to http://www.example.com
My server configuration is like below:
server {
listen 80 default_server;
root /home/vishant/devcenter/wava-v1.1/HTML;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name baetter.l;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#proxy_pass "http://127.0.0.1:3000";
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
i have found similar problem solved using htaccess here
But how can i achieve in nginx??
One of a number of solutions is:
location ^~ /ubb/ {
return 302 /;
}
The ^~ modifier ensures that this prefix location continues to take precedence if you were to add any regex locations in the future. See this document for details.
The return directive is documented here.

Resources