Nginx, try_files proxy and named location with 404 fallback - nginx

I have an odd issue which is only affecting one local app I'm working on - other apps with this approach seem to work fine (Ghost). This is from my Nginx server config:
location #node_proxy {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Host $host;
proxy_redirect off;
proxy_pass http://127.0.0.1:5000;
}
location / {
try_files #node_proxy =404;
}
As I said, I have Ghost running identically to this and it performs fine. However for this config it results in every request being a 404 - it seems to never hit the proxy. I've checked logs and this confirms my suspicions, no entries in the access or error logs.
The app I'm proxying through to in this instance is just a simple Express based node app, so nothing complex. Visiting http://127.0.0.1:5000 I see the expected results.
If I change my config to:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Host $host;
proxy_redirect off;
proxy_pass http://127.0.0.1:5000;
}
It works as expected, however I'd like to make use of the named location so as to avoid having to repeat proxy declarations.
Have I missed something obvious?

Try this kind of hack:
location #root {
...
}
location / {
error_page 418 = #root; return 418; # redirect to #root
}
Seems like it is impossible to jump to named location from a regular one. You also can try try_files #root #root, however Igor Sysoev (nginx's author) says that error_page is better since it uses less resources.

Related

How to deploy Next.js app with nginx to subpath?

My goal
I would like to deploy my Next.js app with nginx to subpath.
My setting is the following:
Nginx config
location /subpath/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://localhost:3030/;
}
next.config.js
module.exports = {
basePath: '/subpath'
}
Trouble
This next.config.js causes mydomain.com/subpath/ to 404.
Otherwise, mydomain.com/subpath/subpath displayed the page. ><
(all other pages also needed /subpath/subpath/xxx)
(Every link href leads to 404, so I confirmed pages by typing /subpath/subpath/ in address bar.)
Other info
The version of next.js is 10.0.4
How can I solve this?
I'm pleased to be provided with the knowledge of deploying next.js app to subpath.
Simplest way is better.
I will appreciate any advice. Thanks in advance.
According to the configurations the problem here is with the trailing slash,
location /subpath/ {
proxy_pass http://localhost:3030/;
}
change it to this,
location /subpath {
proxy_pass http://localhost:3030;
}
and the complete config will be looks like this,
location /subpath {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://localhost:3030;
}
The reason behind is By default Next.js will redirect urls with trailing slashes to their counterpart without a trailing slash. according to the docs
But you've mentioned in the Nginx config that your url should have trailing slash. then it will return 404.

Nginx one domaine add two project - back-end & front-end

Example -
mydomaine.com/back-end/api ---> its back-end
mydomaine.com ---> its front-end
I don't think many people understand this question. Because I don't understand how things will be. If anyone has trouble understanding my question - comment and tell me I will try to write better
anyone help plz..
You have to use a server block configuration of Nginx, try the following configuration:
server {
listen 80;
server_name mydomaine.com;
location /back-end/api {
# This would be the directory where your app static files are stored at
root /var/www/html/;
try_files $uri /index.html;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_redirect off;
proxy_pass http://mydomaine.com;
}
}

Nginx proxy_pass :"invalid number of arguments using http://app_server; or http:/x.x.x.x:8000/uri/;

I am using nginx, django and gunicorn and I keep getting the error invalid number of arguments on the proxy_pass line in my nginx sites-enabled configuration. I believe this is the correct way as per the nginx documentation. Where am I going wrong? I am really stuck.
upstream app_server{
server xxx.xxx.xx.xxx:8000 fail_timeout=0;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host
proxy_redirect off;
proxy_pass http://app_server;
}
I checked the following solution#[similar question][1] but it doesn't answer why the configuration above is not working[1]:nginx invalid number of arguments in "proxy_pass" directive.
I even tried using proxy_pass http://xxx.xxx.xx.xxx:8000; still the same error.
Probably by adding ; to $hppt_host
This is how I solved it. I added proxy_pass and proxy_redirect off first before the other parameters.
location #proxy_to_app {
proxy_pass http://app_server;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
}

Nginx Passing to Servers Based on URI

I have been setting up Nginx on my router, and creating subdomains (with CNAMES) to access various components within my network. It has mostly been fairly easy, until I have come to the cameras which are proving to be a problem.
They are basic IP cameras and to date I had opened each one on a different port. They have basic authentication, and once that has been entered I am presented with a live view.
Like all the other components I have set up so far (and they all work) I started by configuring one:
server {
listen 80;
server_name cam.example.co.uk;
location / {
proxy_pass http://192.168.1.101:2001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Hitting cam.example.co.uk from either LAN or WAN gives me a username and password prompt and then the live view loads.
Since there are 9 cameras, I thought it would be a good idea to use /1, /2, /3 etc. at the end to direct me to each one rather can creating subdomains.
server {
listen 80;
server_name cam.example.co.uk;
location /1/ {
proxy_pass http://192.168.1.101:2001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /2/ {
proxy_pass http://192.168.1.102:2002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
With that I got 404 not found errors, and messages in the logs such as:
"/usr/html/new/index.htm" failed (2: No such file or directory)
Some Googling later I found out that I may need to specify the URI as well in the proxy_pass line, so I changed them to look like:
proxy_pass http://192.168.1.102:2002/new/index.htm;
This then results in the username and password prompt, but when the credentials are entered, all I am left with is a blank screen. It worked fine when it was just location / so no idea why nothing is showing now.
I have a feeling that it is putting the URI in somewhere, but I have no idea where/why or what to do about it.
EDIT
Been Googling and trying various things:
location /1 {
resolver 127.0.0.1;
set $backend "http://192.168.1.101:2001/new/index.htm";
proxy_pass $backend;
proxy_buffering on;
proxy_redirect http://192.168.1.101:2001/new/index.htm http://cam.example.co.uk/1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Then going to this in the browser cams.example.co.uk/1 brings up the username and password prompt, but then displays a blank page. Looking at the Chrome developer tools I can see unexpected token errors, and it looks like it isn't loading the .js files properly.
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
Try this:
server {
listen 80;
server_name cam.example.co.uk;
location /1/ {
proxy_pass http://192.168.1.101:2001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect http://192.168.1.101:2001/ http://cam.example.co.uk/1/;
}
location /2/ {
proxy_pass http://192.168.1.102:2002/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect http://192.168.1.101:2002/ http://cam.example.co.uk/2/;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

nginx rewrite rule for redirection

I have two apps running on host1:7000 and host2:7000. I am fronting the two hosts by an nginx reverse proxy, where I want mydomain.com/admin to point to host1:7000/portal and mydomain.com/user to host2:7000/portal.
I have written the following config
listen 80;
server_name mydomain.com *.mydomain.com;
location ~ ^/admin/(.*)$ {
proxy_pass $scheme://<IP-ADDRESS>/$1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
I can get to mydomain.com/admin to be redirected to host1:7000/portal but when the app redirects from host1:7000/portal on to host1:7000/login via relative path, in the browser I see mydomain.com/login. What do I need to do to get the second redirect go mydomain/admin/login?
Why do people use regexps for no reason and have all kind of problems with it?…
location /admin/ {
proxy_pass http://host1:7000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
This will automatically strip /admin/ from proxied request and prepend it in Location header (which is used in redirect).
See proxy_pass and proxy_redirect docs.

Resources