nginx with two node aplications with socket IO - node.js

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.

Related

Facing an issue while configuring nginx. Getting empty pages while going to a specific path

Firstly I explain my issue here.
I have 3 react apps running on 4040 4141 4242 respectively.
Initially, I created 3 servers with three ports like
server {
listen 3000;
location / {
proxy_pass http://localhost:4040;
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;
}
}
server {
listen 3001;
location / {
proxy_pass http://localhost:4141;
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;
}
}
server {
listen 3002;
location / {
proxy_pass http://localhost:4242;
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;
}
}
This was working fine. But I need to serve all these three apps from one port.
eg: http://localhost:8080/ab should redirect to port 4040 and
http://localhost:8080/ac should redirect to port 4141 like so.
Now I tried this way
server {
listen 8080;
server_name localhost;
location /examcontrol {
proxy_pass http://localhost:4040;
}
location /student {
proxy_pass http://localhost:4141;
}
location /staff {
proxy_pass http://localhost:4242;
}
}
This will redirect to the port 4040,4141,4242
but only some tags are visible like <title>,<script>,<noscript> etc no other contents is shown in the browser. (if I change /examcontrol to / I get the correct response and redirected to 4040, but no idea why these paths not working :( )
Please help if anyone knows this to configure it correctly.
I'm pretty sure you could fix this easily by changing your proxy_pass directive (note the trailing /):
proxy_pass http://localhost:4242/;

NGINX Run multiple application on same port with different route path

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.

Socket.io 404 on nginx

So I have been looking through a lot of posts, websites, and have still not gotten this problem fixed.
I have previously had a project running on my server, including socket.io, without problems.
But now that I am uploading this new project to the server, it seems that the socket.io always returns 404.
Sorry if I have overlooked something obvious here.
Nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
proxy_pass http://localhost:3001;
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 /demo/ {
proxy_pass http://localhost:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
location /socket.io/ {
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_pass_request_headers on;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
My socket.io server:
const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server);
server.listen(3000);
Client (Pug):
script(src="/socket.io/socket.io.js")
Note that the code above is what I have ended up with after reading through a lot of questions on here, on serverfault, aswell as blog posts several places.
I don't remember using a second location "tag" (is that what it is called?) for socket.io for the first one I used.
The proxy pass should be:
location /socket.io/ {
proxy_pass http://localhost:3000/socket.io/;
...
}
Please see https://medium.com/#ibraheemabukaff/how-to-proxy-websockets-with-nginx-e333a5f0c0bb for details...
Giving your Nginx the server name localhost and then trying to proxy_pass to localhost is probably not going to do you any favours.
Try getting rid of the server_name localhost; entirely and changing all these:
proxy_pass http://localhost:xxxx;
to this:
proxy_pass http://127.0.0.1:xxxx;
Or, if you want to do it properly then outside of your server block create an upstream directive:
upstream socketserver {
server 127.0.0.1:3000;
}
and change your proxy_pass directives to:
proxy_pass http://socketserver/;

nginx settings are correct but fails to proxy: logs show zero error/activity

I have a domain that would hit my nginx proxy to be routed to their nodejs hosted site locally on a different port. About a month ago it stopped working:
DNS A records are fine and forward to correct IP
Website works if I go to ip address with correct port(3100)
Hell, it even works if I type domain.com:3100.
It's an Ec2 instance and port is open to all IP addresses
Here are the config file in the sites-enabled/site-available folder:
server {
listen 80;
server_name www.cpcarpet.com cpcarpet.com;
access_log /var/log/nginx/cpcarpetaccess.log;
error_log /var/log/nginx/cpcarpeterror.error.log debug;
location / {
proxy_pass http://localhost:3100/;
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've restarted the nginx service quite a bit and updated the package but nothing seems to work. Any ideas?
So to clarify what does work:
IP address:3100 works! Ok!
cpcarpet.com:3100 works!(So A records are set correctly) Works! Ok!
www.cpcarpet.com or cpcarpet.com? Does not work! Log files show no access/errors either.
Try this. Hope this helps you.
server {
listen 80;
server_name cpcarpet.com;
location / {
proxy_pass http://localhost:3100;
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;
}
}

How to Create a NodeJS http Proxy

I tried proxying my nodejs application running at http://localhost:7000 to domainname.com/api using nginx config but domainname.com/api works perfectly however domainname.com/api/info is not
Here is my nginx config
server {
listen 80;
server_name domainname.com
location /api/ {
proxy_pass http://localhost:7000;
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;
} }
Figured out the issue. trailing slash is the one required as shown
proxy_pass http://localhost:7000/;

Resources