I am using socket.io on my nginx server.
I followed the doc from nginx to configure my server (http://nginx.com/blog/nginx-nodejs-websockets-socketio/):
location = / {
proxy_pass_header Server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
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_set_header X-Forwarded-Proto $scheme;
proxy_pass http://frontend;
proxy_intercept_errors on;
error_page 501 502 503 504 /50x.html;
}
upstream frontend {
server 127.0.0.1:7300;
}
However I still get the following error in my browser:
WebSocket connection to 'ws://<MY DOMAIN>/socket.io/?EIO=3&transport=websocket&sid=oway-6XKt7_HvqfQAAAC'
failed: Error during WebSocket handshake: Unexpected response code: 400
Everything works fine locally so I get the problem comes from my nginx.
What should I change to make it work?
Many thanks
Related
I have a dockerized Nginx reverse proxy to a nodejs app which is supposed to serve generated PDFs based on data entered.
Here's the current state of nginx.conf
# nginx.conf
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
# bot
location / {
proxy_pass http://frontend:80;
proxy_http_version 1.1;
proxy_set_header X-Forwareded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_buffering off;
}
location /api {
proxy_pass http://backend:3000;
proxy_http_version 1.1;
proxy_set_header X-Forwareded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_buffering off;
}
location /socket.io {
proxy_pass http://backend:3000;
proxy_http_version 1.1;
proxy_set_header X-Forwareded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
}
location /files {
proxy_pass http://backend:3000;
proxy_http_version 1.1;
proxy_set_header X-Forwareded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
In the node app, the correct directory is mapped to the /file endpoint:
app.use('/files', express.static(path.join(__dirname, 'storage')));
However, when trying to load a PDF in browser, I get the following error message:
This site can’t provide a secure connectionlocalhost sent an invalid response.
ERR_SSL_PROTOCOL_ERROR
And the nginx container logs show:
172.23.0.1 - - [24/Oct/2019:16:42:39 +0200] "\x16\x03\x01\x02\x00\x01\x00\x01\xFC\x03\x03\xF7\x01\xBB\x1E\xD2\xAF*\xB7\xFB\xB96b4\xEE\xEB\xC9\x18u\x94-`(\xBA\xB9'\xC18o\xA0n\xA0\xDA \xB2l~n\xDFd\xB9\x02C/]\x9C3\xD5u\x19\xA7\xE3c\x1D\xFD\x06\xEBv\x0B\x88x\x8FW\x9As\x1F\x00\x22\xDA\xDA\x13\x01\x13\x02\x13\x03\xC0+\xC0/\xC0,\xC00\xCC\xA9\xCC\xA8\xC0\x13\xC0\x14\x00\x9C\x00\x9D\x00/\x005\x00" 400 157 "-" "-" "-"
The goal is to be able to test everything on localhost (hence no SSL), production environment would probably include an additional Nginx reverse-proxy in front of this one which handles certifcates etc.
I'd be very grateful for any pointers regarding how to serve PDFs in both environments.
Thanks in advance!
i have a laravel app and i want all requests to domain.test/api to be proxied to nodeJs but only if it is an xhr request. meaning that if a user types in a browser domain.test/api i want to give him a 404 but if the request is made with ajax i want to give him the response.
the following configuration proxies all:
location ~* ^\/api(.*)$ {
proxy_set_header Host $host;
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 X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:8081;
}
Is what i want to do possible using nginx? if so, please do suggest your solutions?
I am trying to set up a Nginx as a reverse proxy to access multiple NodeJS apps running on the same server.
I have my nodeJS apps running with PM2 and it all seems fine:
My nodeJS app is the simple nodeJS app generated with express-generator, so it is supposed to be running on port 3000.
I have also set up my Nginx with the following config
server {
listen 1004;
server_name pumadashboard.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
However when I curl 127.0.0.1:1004 I get a badGateway error from Nginx. I am also not able to access pumadashboard.com from anywhere on my local network, it just loads until timeout.
What do you get if you do:
curl http://locahost:3000
This should give back a response and like that you will understand if the application started properly.
This nginx configuration works for me
upstream pumadashboard.com {
server 127.0.0.1:3010;
}
server {
listen 80;
server_name pumadashboard.com;
root <path to your node application>;
access_log /var/log/nginx/your-access.access.log;
error_log /var/log/nginx/your-error.error.log;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
location / {
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://pumadashboard.com/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
Try adding
proxy_set_header X-Forwarded-Host $http_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-Port 80;
to your location / block
I'm trying to get webpack-dev-server running inside a Docker container then accessing it through a NGINX host. The initial index.html loads but the Web Sockets connection to the dev server cannot connect.
VM47:35 WebSocket connection to 'ws://example.com/sockjs-node/834/izehemiu/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
I'm using the following config.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream webpack_dev_server {
server node;
}
server {
server_name _;
listen 80;
root /webpack_dev_server;
location / {
proxy_pass http://webpack_dev_server;
}
location /sockjs-node/ {
proxy_pass http://webpack_dev_server/sockjs-node/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host; # pass the host header - http://wiki.nginx.org/HttpProxyModule#proxy_pass
proxy_http_version 1.1; # recommended with keepalive connections - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version
# WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
Proxy pass should be ip and port of your webpack-dev-server container and you need proxy_redirect off;
location /sockjs-node {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://node:8080;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
Also don't forget to add poll to your webpack-dev middleware
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}
I have two node js applications I am running on the same box and I would like for it to run the first node js app for all routing except if the url is www.domain.com/blog to go to the other node js application. Is this even possible with this setup or do I have to setup subdomains and use nginx or something?
You can achieve this using nginx as a reverse proxy.
Assuming you have your blog node process running on port 3000 and another node process on 3001 a simple config would look like;
upstream blog {
server 127.0.0.1:3000;
}
upstream other {
server 127.0.0.1:3001;
}
server {
listen 80;
server_name www.domain.com;
location /blog {
proxy_pass http://blog;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
location / {
proxy_pass http://other;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
}