I have multiple node-red application running on different servers using a single domain name through Nginx like:
server {
server_name abc.com www.abc.com; #main website
root /var/www/web;
index index.html;
location /server/demo{
proxy_pass http://xx.xxx.xx.xx:1880/server/demo; #node-red app 1
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /server/demo2{
proxy_pass http://yy:yyy:yy:yy:1880/server/demo2; #node-red app 2
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /server/demo3{..... #node red app 3
......
......
I'm not sure but I think the same session or cookie is shared by all these instances so I'm not able to log in to multiple instances of my application in the same browser/machine(even though they are running on different servers). If I login to one node-red instance and then in a new tab if I log in to another I get logged out of the first one.
For example, I'm not able to login to abc.com/server/demo1 and abc.com/server/demo2 simultaneously but I am able to login to multiple instances using their IP addresses http://xx.xxx.xx.xx:1880/server/demo and http://yy:yyy:yy:yy:1880/server/demo2.
Is there a way to set up HTTP cookie for each location object in Nginx if that itself is the issue here?
or
A better approach to achieve this functionality?
Node-RED uses the default express-session plugin which defaults to setting all the cookies on the root path /.
I've not tried this, but it looks like nginx's proxy_cookie_path directive might be able to do what you want. The docs are here.
If I've understood the docs properly then an entry like this for each instance should work:
location /server/demo{
proxy_pass http://xx.xxx.xx.xx:1880/server/demo; #node-red app 1
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_cookie_path / /demo;
}
location /server/demo2{
proxy_pass http://yy:yyy:yy:yy:1880/server/demo2; #node-red app 2
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_cookie_path / /demo2;
}
Related
After installing Nginx and running it.
`location / {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}`
but it wants not run in the droplet Ip address, it's just run locally.
I have two applications, app1 is developed in reactJS and app2 in angularJS sharing same login session,
- Application 1
http://application-1:1234/
- APplication 2
http://application-2:2345/
My needs is to have a seemless navigation between both apps, as they share the same login credentials.
I have created NGINX reverse proxy configuration,
server {
listen 8080;
server_name http://global-ip:8080;
location / {
proxy_pass http://application-1:1234;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /application-2 {
proxy_pass http://application-2:2345;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
As the above configuration is working for only, First default root path. The other /application-2 is not able to redirect to specified path.
Any help will be appreciated.
Thanks
Praveen T
As a quick hack, try either
location /application-2/ {
proxy_pass http://application-2:2345/;
...
}
or
location /application-2/ {
rewrite ^/application-2(.*) $1 break;
proxy_pass http://application-2:2345;
...
}
but you'd better build you angular app according to your URI prefix, see instructions here. Then your original config should work as expected.
I have two applications running on the same machine. One listens on port 8080 and the other on 11180. SSL connection to the application on port 8080 works but i am having trouble setting up the other one.
To separate the requests heading to each of the applications, one is available at https://example.com and the other at https://example.com/v2
As I said, going to https://example.com works as intended, but going to https://example.com/v2 serves the correct html files but connects to the same server as going to https://example.com
I honestly have no idea what i am doing with nginx, but my config looks like this.
server {
listen 443 ssl;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /v2/ {
proxy_pass http://127.0.0.1:11180/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}
location /socket.io {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_redirect off;
}
}
It is worth mentioning that the first app listens on 8080 and its socket io on 8081, as for the second app, everything listens on 11180
Thanks a bunch in advance
I ended up adding another server block to the nginx config, and accessing it via www.example.com:port.
As the accepted answer say you could create a new server block... or you can even create a new conf file in the sites-available folder for the new domain with a new server block. Dont forget to link them to sites-enabled.
I'm trying to set up a reverse proxy using nginx for a nodejs application. My node application currently runs on port 8005 of the example.com server. Running the application and going to example.com:8005 the application works perfect. But When I tried to set up nginx my application seems to work at first by going to example.com/test/ but when I try and post or get requests the request wants to use the example.com:8005 url and I end up with a cross origin error, CORS. I would like to have the request url reflect the nginx url but I'm having no luck getting there. Below is my nginx default.conf file.
server {
listen 80;
server_name example;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /test/ {
proxy_pass http://localhost:8005/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
There got to be some way to tell nginx about whichever app you are using.
So for that, either you can prefix all the apis with say test(location /test/api_uri), and then catch all the urls with prefix /test and proxy_pass them to node, or if there is some specific pattern in your urk, you can catch that pattern with regex, like suppose, all the app1 apis contain app1 somewhere in it, then catch those urls using location ~ /.*app1.* {} location ~ /.*app2.*, make sure that you maintain the order of location.
Demo Code :
server {
...
location /test {
proxy_pass http://localhost:8005/; #app1
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /test2 {
proxy_pass http://localhost:8006/; #app2
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
...
}
Other Demo for regex,
server {
...
location ~ /.*app1.* {
proxy_pass http://localhost:8005/; #app1
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ~ /.*app2.* {
proxy_pass http://localhost:8006/; #app2
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
...
}
I installed nginx to serve multiple nodejs apps
On my server I have 2 apps myapp and pm2-web
the nginx config look like this
http {
# .... logs, gzip ... etc
server {
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /pm2 {
proxy_pass http://localhost:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
my app runs fine but when I try to access /pm2
I get the following error
Cannot GET /pm2
when pm2-web is not running I get 502 Bad Gateway
But I can still access pm2 from http://IP:9000
The /pm2 part of the URL is being passed through to your Node application, where it does not match any paths.
ie, your pm2 app is running on 9000, but you are trying to access http://localhost:9000/pm2 which doesn't exist.
Include a trailing slash in your proxy pass URL to ensure /pm2 is not being included:
location /pm2 {
proxy_pass http://localhost:9000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}