Removing start of path from nginx proxy_pass - node.js

To remove the use of ports on several of the applications running on this server, I've been using nginx's proxy_pass to do this. However, for some reason the actual url is being passed to the application. Is there a way so that it thinks /panel is really just /?
location /panel {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8082/;
}

You need to add the trailing slash
location /panel/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8082/;
}

Related

Duplicate Location "/" Nginx Error Backend And Frontend

I'm running an Express server on port 5000 and a React app on 8080.
My idea was to just direct traffic based on location (uri), but I've been getting a duplicate location "/" error with this partial insert into the AWS Beanstalk ngnix config:
location / {
root /var/app/current/build/;
try_files $uri /index.html;
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /socket-io {
proxy_pass http://127.0.0.1:5000/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Real-IP $remote_addr;
proxy_ssl_session_reuse off;
proxy_cache_bypass $http_upgrade;
}
This gets included in a standard ngnix.conf which looks roughly like this: https://gist.github.com/alextanhongpin/00da7f551969f052e57f3c4dcd9ac4b0
This is some combination of this answer and this other one since AWS documentation keeps changing.
On EC2, the file /etc/nginx/conf.d/elasticbeanstalk/00_application.conf is the file that appears to get appended on the main nginx.conf file with the line /etc/nginx/nginx.conf.
HOWEVER
The latest documentation says having .platform/nginx/conf.d/your_custom.conf in your working directory is the real nginx extension file.
I had to manually change 00_application.conf on EC2 in order for the duplicate error to go away, it appears this file stayed default even when redeploying with the custom config files, and that file happens to have a location / {}.
So I would recommend the container_command method or manually changing it for now.

Serve express dynamic URL using NGINX with Nodejs reverse proxy

I am using Express for serving static files and dynamic URLs on my nodejs app. I am generating a URL based on the :id in Mongodb and then on NGINX I am using Nodejs reverse proxy to serve this path to the user but it doesn't work whatever I try. It always says "CANNOT GET.."
I am running NGINX 1.14.2, Node 8.14.0. I have tried NGINX rewrite directive and various wild cards ( ~, ~^, ., etc.) but none of them have worked. I have included the current express code and NGINX config below. Any help is hugely appreciated
Express
router.get('/dynamic-uri/:fooId', function (req, res) {
res.render('index');
});
The resultant URL with :fooId is
https://example.com/myNodeApp/dynamic-uri/5c35e51228122504b57126d7
NGINX Config
location /myNodeApp/dynamic-uri {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}
Of course the :fooId doesn't exist as a static path and to serve static files i have the following block which works fine
location ~* /myNodeApp/(static|img|media|app|css|js) {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}

NGINX server with 2 NodeJS Apps

I will make it short without introductions ..
I'm having a serious issues with NGINX configuration (on google cloud) to make 2 nodejs apps working on the same domain with different PORTS
let's say app1 is working on port 3002, app2 working on port 3003
app1
location / {
root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_redirect off;
}
app2
location /app2 {
root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3003;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_redirect off;
}
when I surf www.example.com/app2, I get 404 page
I know some of u will say that this Q has been asked before, believe me I've tried all possible solutions on stackoverflow .. non has worked with me
Note: app1 location it has to be the main domain so (/) the main domain URL without path
I believe your code does not use relative paths, thats why you are getting this error, add this line:
rewrite ^/app2(.*) /$1 break;
and no root required for proxy pass, your new code shall look like this:
location /app2 {
#root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3003;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_set_header X-NginX-Proxy true;
#proxy_redirect off;
rewrite ^/app2(.*) /$1 break;
}
The first location block captures requests for all requests of your domain, leaving the second block never used. Put the second block before the first one and it should work.

NGINX Reverse Proxy Multiple Domains

Maybe this question got already answered but I can't find the solution for my problem resp. no solution worked for me.
I'm pretty new with NGINX and I've now set up a Raspberry Pi 2 Model B as a Reverse Proxy with NGINX in our company-network. RPi OS is Raspbian v7.
The Hostname (raspberry) of this Reverse Proxy is set over our DHCP Server with a reserved IP.
At the moment I'm able to access another internal Server (resp. its webpage) when I enter the URL for my raspberry in my webbrowser - this works fine.
nginx.conf configuration - working server block in http block:
server {
listen 80 default_server;
server_name webpage;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass <link for mentioned webpage>;
}
}
Now I would like to be able to access other public/internal websites by entering URLs like:
raspberry/stackoverflow (proxies to stackoverflow)
raspberry/google (proxies to google)
raspberry/facebook (proxies to facebook)
in my webbrowser.
I've tried to use different location blocks in nginx.conf like:
server {
listen 80 default_server;
location /webpage {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass <link for mentioned webpage>;
}
location /stackoverflow {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass <link-stackoverflow>;
}
location /google{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass <link-google>;
}
#and so on.....
}
But this doesn't work, if I enter raspberry/google I get a 404 error from google:
The requested URL /google was not found on this server
This means it searches for https://www.google.com/google instead of just https://www.google.com/
How could I realise my plan? Any help is greatly appreciated :)
P.S. Stackoverflow doesn't allow me to post more than 2 links yet so I wasn't able to write real links (see <>).

Nginx + nodejs app location

it's very short question, but i'm overwhelmed. I need nodejs app run under domain.com/nodeapp/.
The problem is - it works correct if i write domain name like: domain.name/nodeapp/ so when i'm go to domain.name/nodeapp - corrupted version loads.
I need nginx to redirect correctly to location with /nodeapp/
Now i'm using next config:
location /nodeapp {
proxy_pass http://localhost:20100;
rewrite ^/nodeapp/?(.*)$ /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
How can i do it correct? Thanks!
This will do
location /nodeapp {
proxy_pass http://localhost:20100;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Resources