ActionCable with SSL not working in production environment - nginx

I am using ActionCable in rails 5.2 and deployed the code on AWS with nginx server.
Previously WebSocket was working when I was working on http but when i have implemented SSL, it stopped working. For SSL I have implemented load balancer in AWS. I am using Unicorn as rails application server.
my ActionCable url is:
SOCKET_URL: wss://example.com/cable
Started GET "/cable/"[non-WebSocket] for 182.74.85.106 at 2019-10-30 14:32:05 +0000
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 182.74.85.106 at 2019-10-30 14:32:05 +0000
configuration in my production.rb file is:
config.action_cable.url = ENV["SOCKET_URL"]
ActionCable.server.config.disable_request_forgery_protection = true
my nginx conf is:
upstream unicorn {
server unix:/usr/share/nginx/html/demo_app/shared/tmp/unicorn.demo_app.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html/demo_app/current/public;
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

You can paste below lines in your nginx.conf and try.
location /cable {
proxy_pass http://unicorn/cable;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}

Related

Strange HTTP 401 response with nginx in combination with WildFly

I installed a WildFly 26.0.1 together with nginx as a reverse proxy.
Everything seems to work correctly.
Also the deployment of small WebApp war files within WildFlys admin console works.
But there is a problem when deploying large war files.
I have already set client_max_body_size to 100M!
The effect is the following:
While deploying the nginx access.log there shows up an endless loop
POST /management-upload HTTP/1.1" 401 77
Again and again
On the client side the request hangs.
The WildFly Log shows no start of deployment.
While with small war files it says:
POST /management-upload HTTP/1.1" 200 68
btw: When accessing the WildFly directly (not via the nginx proxy) the deployment works
also with large war files
This is my nginx config:
(Replacing my domain with example.com)
server {
server_name www.example.com example.com;
listen 80;
listen [::]:80;
client_max_body_size 100M;
location / {
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_pass http://127.0.0.1:8080;
proxy_read_timeout 90s;
}
location /console {
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_pass http://127.0.0.1:9990/console;
proxy_read_timeout 90s;
}
location /management {
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_pass http://127.0.0.1:9990/management;
proxy_read_timeout 90s;
}
}

Problem congifuring Nginx to visualize Swagger UI

I have a containerized dotnet service including swagger UI that I can locally run on localhost:7060/swagger/index.html.
I have a problem configuring nginx to point to it. Here is my weaplan.conf file that nginx detects
server {
Listen 80;
location /swagger {
root /var/www/html/weaplanservices/DataHandlerAPI;
proxy_pass http://127.0.0.1:7060;
try_files $uri $uri/ /index.html;
}
}
Note: the project exists in the exact indicated root and the containerized app works correctly
I solved this issue by reconfiguring nginx this way to serve Swagger UI:
server {
Listen 80;
location /swagger {
root /var/www/html/weaplanservices/DataHandlerAPI;
proxy_pass http://127.0.0.1:7060;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Cookie Rewrite with NGINX

Okay so I've set up a nginx server that proxies to another 2 servers with load balancing. The only thing now missing are the cookies.
I've been searching numerous forums and questions regarding the rewriting of cookies. Can anyone please give me insight as to how to fix this issue?
The web application deployed to the 2 servers are written with Vaadin.
The 2 servers are running TomEE on port 8080 and 8081 for example.
I'm rewriting through nginx from easy.io to server1:8080 and server2:8080.
Refer to image below: when navigating to server1:8080/myapplication all my cookies are available.
https://ibb.co/X86pvCq
https://ibb.co/0M0GjCt
Refer to image below: when navigating to http://worksvdnui.io/ my cookies are not available.
https://ibb.co/qBkBRqb
I've tried using proxy_cookie_path, proxy_set_header Cookie $http_cookie but to no avail.
Here's the code:
upstream worksvdnuiio {
# ip_hash; sticky sessions!
ip_hash;
# server localhost:8080;
server hades:9090;
server loki:9090;
}
server {
listen 80;
listen [::]:80;
server_name worksvdnui.io;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location /PUSH {
proxy_pass "http://worksvdnuiio/test.qa.gen/PUSH";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering off;
proxy_ignore_client_abort off;
proxy_read_timeout 84600s;
proxy_send_timeout 84600s;
break;
}
location / {
proxy_pass "http://worksvdnuiio/test.qa.gen/";
proxy_cookie_path /test.qa.gen/ /;
proxy_set_header Cookie $http_cookie;
proxy_pass_request_headers on;
}
}
Any insight would be VALUABLE!
Thanks in advance.

WebSocket connection to 'ws://<ip>..failed: Error during WebSocket handshake: Unexpected response code: 502

I'm using node,socket.io,nginx for socket connections for a http based simple application. below is my nginx.conf where i configured the websockets, my socket io is present in socket/socket.io. Strange thing is that the same config works perfectly when I run the node application in localhost and nginx with port 80. Not sure why it is failing in linux which real IP addresses.
http {
upstream io_nodes {
ip_hash;
server IP:PORT; --> My application's IP and port
server IP:PORT;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 1000;
# HTTP Server
server {
listen 80;
server_name IP;
server_tokens off;
#proxy_ssl_verify off;
# prevents 502 bad gateway error
large_client_header_buffers 8 32k;
location /dummy/ {
root html;
index dummy/index.html;
}
location /socket/socket.io {
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;
# prevents 502 bad gateway error
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_pass http://io_nodes/;
proxy_redirect off;
proxy_http_version 1.1;
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;
}
#location /socket.io/ {
# proxy_pass http://io_nodes/socket.io/;
#}
location /assistlog/socket.io {
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;
# prevents 502 bad gateway error
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_pass http://10.73.52.187:4444/;
proxy_redirect off;
proxy_http_version 1.1;
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;
}
}
}
and this is the console error in chrome
WebSocket connection to 'ws://IP/socket/socket.io/?pathName=%2FDEMOUI%2Findex.html&EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 502
Please suggest a solution.
Check list:
First check whether your browser is supporting web sockets using https://websocket.org/echo.html
Then, if you are under organisation proxy, then you need to by pass proxy for local addresses.
Let us say, your organisation proxy IP is some x.x.x.x. You need to by pass some addresses(from the application where you need wss/ws connecctions) to NOT to use your org proxy.
Example in Mac, Chrome: Settings, search for proxy, and there you can see "bypass proxy settings for these hosts&domains". Add your application IP there

ghost dosen't work in subdomain

i have VPS server work in digitalocean with nginx and ubuntu 12.4 LTS 64bit, i try to make ghost blog work in my subdomain blog.csbukhari.com but it dose not work.
this is my conf file in nginx
server {
listen 80;
server_name blog.csbukhari.com;
location / {
expires 8d;
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 Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_read_timeout 5m;
proxy_connect_timeout 5m;
proxy_pass http://127.0.0.1:2368;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
and i add the subdomain blog in dns as A record
You can see my example conf file here but yours looks right.
I assume you have restarted nginx and you have Ghost started and listing on port 2368?

Resources