I am very lost at the moment, so much so that I actually signed up because of my problem. So here we go: I am running a System with Plex and I am trying to get a node.js Backend with socket.io to work that is running behind Nginx.
The proxy pass for my HTML (I am using ejs to be precise, not sure if relevant) hosted via express works like a charm. But I get a 404 error when my website tries to connect to my backend via socket.io.
My Nginx directives as configured via Plex:
location = / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:50090;
}
location /socket.io/ {
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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:50090/socket.io/;
}
My client connecting to the backend:
var socket = io.connect('https://subdomain.mydomain.com', {secure: true});
My Backend listening:
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io').listen(http);
http.listen(50090, "127.0.0.1");
There is a lot about this on the internet, but it seems like I tried everything by now and nothing worked. Likely because I am very new to this.
Related
I deploy a Mean app with nodejs and express.
I made a reverse proxy with nginx.
`
location /soc/ {
root /capza_app/back/;
index index.js;
# proxy_set_header X-Real-IP;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header AccesControl-Allow-Origin *;
proxy_pass http://ip:3000/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
`
I call my api in front here:
apiUrl = 'https://mydomain/soc/transaction/'
After all go in back in my index.js:
app.use('/soc/transaction', TransactionController);
My index send in my controller.
I have 404 error.
Without the reverse proxy, i have Mixed Content: The page at https as loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint http.
maybe routes problems but I don't know what i am do wrong.
Thanks
I try to change my route, chang my reverse proxy
My website has the proxy server created using express nodejs with nginx. The problem I am having, and cant seem to figure out is that I want to forward all the original users details and not show any proxy server details to the final destination server.
Scheme:
User -> my website -> proxy server -> final server to retrieve the data.
Now, no matter what configs I change in nginx, seems like the final server always identifies my proxy server isp, geo etc and not the users isp. If I access the final server directly without the proxy, the final server will show users isp.
How do I make sure that the proxy server is not being detected as "original" requester to the final server?
Some code for better understanding:
Nginx proxy:
location / {
proxy_pass http://x.x.x.x.x;
include proxy.conf;
}
nginxconfig:
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
# Proxy headers
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
Requests inside the node (to call final server) made like this (just a sample):
app.post('/getStats', (req, res) => {
const url = request('url')
req.pipe(url).pipe(res)
})
For example. User request http://user.dist.com, the request will firstly arrived at nginx server,
upstream a.hello.com {
server 10.243.26.104:8800;
}
server
{
listen 80;
server_name user.dist.com;
location /
{
proxy_pass http://a.hello.com;
}
}
and the server a.hello.com is running at Node.js, the Node sever want to get the real origin request host which should be user.dist.com, but now, the Node server get a.hello.com, so how to get the origin host?
you can use proxy_set_header-
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
I'm using NGINX to handle my Nodejs servers and now I want to start using socket.io on them, doing research before starting to play with socket.io i found Sailsjs which I grew rather found of and now currently using.
The problem I meet though, is that the client can't connect through NGINX on socket.io, or sails.io.
This is the URL path for my current APP:
https://localhost/economy
And the url for sails.io is, as standard:
https://localhost/economy/assets/js/dependencies/sails.io.js
Now!
Using FileSeek I found out where sails.js use "require("socket.io")", which is in the "loadSocketIO.js" (node_modules\sails\lib\hooks\sockets\lib). I edited the file to say:
var io = sails.io = sails.ws =
SocketServer.listen(sails.hooks.http.server, {
resource: 'economy',
logger: {
info: function (){}
}
});
adding the resource: 'economy', line.
Then on the client side I did the same:
var socket = io.connect('https://localhost/economy/socket.io', { resource: 'economy' });
I've been playing with this and tried different ways, like "economy/socket.io", the full socket.io path etc.
Any help, is a lot of help!
I've been struggling for a day now so I figured i'd ask for some help :)
You shouldn't need to change anything on the Sails side. Try reading up on proxy_pass for your location directive in Nginx.
http://nginx.org/en/docs/http/websocket.html
This is an excerpt from the above link:
location /chat/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:1337;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
SOLUTION!
Run everything as it is, the only part you have to edit is in NGINX.
set a server block with the servername as economy.localhost/, and you're done :)
Remember to link economy.localhost to 127.0.0.1 or set a cname if this is a production server :)
The example showed here worked for me.
Basically for nginx WebSocket support you need to include the following config.
server {
server_name app.domain.com;
location / {
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_pass http://localhost:8080;
}
}
I run my node app on localhost:3000 and it is serving a default page for the route /. If I access http://localhost:3000 the default page is displayed accordingly. I have also running a Nginx server that is basically configured as followed:
server {
listen 80;
server_name localhost;
location /node_app {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
If I run http://localhost/node_app now, my node app throws an error saying that it cannot find route /node_app.
How can I configure either my node app or the nginx server in a way that I can access the app by calling http://localhost/node_app, yet the app itself thinks it is at /?
Update
If I add a / to http://127.0.0.1:3000 it is actually matching /node_app to the / route. But now every stylesheet for instance within the default page is now pointing to the wrong path.
After experimenting a bit around I finally got the configuration right to work exactly how I wanted the server to work:
server {
location /node_app/ {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Lesson learned: Remember the slashes!