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

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.

Related

nginx proxy_pass with rewrite in url not keeping the original paths

I'm trying to setup nginx to forward incoming requests in the following way:
http://localhost/service -> http://localhost:8080
http://localhost/service/foo -> http://localhost:8080/foo
Now I am able to achieve the first line with the following config:
...
upstream service {
server service:8080;
}
...
location /service/ {
server_name_in_redirect off;
proxy_pass http://service;
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;
}
but when I add extra paths (e.g. /foo) the url is rewritten in this way
http://localhost/foo/
therefore I will never be able to reach localhost:8080/foo. Any ideas on how to make this work?
Thanks in advance

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 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.

Nginx, try_files proxy and named location with 404 fallback

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.

NGINX - proxying to a different Wordpress site while retaining URL

I currently have two WORDPRESS websites sitting behind an NGINX proxy cache:
htxtp://local.example.com
htxtp://local.example.org
I want to access a URL from the first site but serve it from the second site whilst not losing the URL structure of the first (to allow website2.com to see the website1.com cookies).
For example:
I want:
htxtp://local.example.com/somepage/
To proxy the page built at:
htxtp://local.example.org/somepage/
BUT I don't want the URL to BE htxtp://local.website2.com.
My NGINX config is as follows:
server {
listen 80;
server_name local.example.com;
access_log logs/local.example.com.access.log;
error_log logs/local.example.com.error.log;
location /somepage {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host local.example.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host local.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Any suggestions? I am trying to work out where the actual redirect is happening.

Resources