Create multiple websocket server with one port number - websocket

I am using netty 4.0.20 I want to create different websocket servers on the same port using different urls
for example,
wss://localhost:1234/PathA
wss://localhost:1234/PathB
wss://localhost:1234/PathC
is that possible?

Yes, this is possible with using reverse proxying, which can be done with Nginx.
This will require one additional server in your setup.
First you have to setup each server to listen to a different port and then you need the front end server to listen to your desired public port (in your case, this is 1234).
So lets say you have the following servers
Nginx listening at 0.0.0.0:1234
Netty that serves /PathA and listens at 0.0.0.0:1235
Netty that serves /PathB and listens at 0.0.0.0:1236
Netty that serves /PathC and listens at 0.0.0.0:1237
Now what you have to do is write an Nginx configuration file that will upgrade the connection from HTTP to Websocket and then reverse proxy each path to its corresponding server. An example configuration file that could do the job for you is the following.
{
listen 1234;
server_name localhost;
location ~PathA/$ {
proxy_pass http://localhost:1235;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "upgrade";
}
location ~PathB/$ {
proxy_pass http://localhost:1236;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "upgrade";
}
location ~PathC/$ {
proxy_pass http://localhost:1237;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "upgrade";
}
}

Related

nginx reverse proxy for elasticsearch

I used nginx proxy for elasticsearch http://localhost:9200 as below shown
location /elasticsearch {
proxy_pass http://localhost:9200;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
but when i try to run https://<nginx_server>/elasticsearch
I got below error, any idea why? maybe I need change basePath and rewriteBasePath at elasticsearch.yml?but i did not find this option. I could not use subdoamin (e.g elasticsearch.domain_name) , i am sure if I can use suddomain for servername at nginix conf, it will be no problem at all
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"elasticsearch","index_uuid":"_na_","index":"elasticsearch"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"elasticsearch","index_uuid":"_na_","index":"elasticsearch"},"status":404}

Tell lighttpd used protocol (HTTPS) when Nginx reverse proxy is in front

I have a Nginx reverse proxy redirecting to a lighttpd server in the same machine. This reverse proxy works on HTTPS so I want to tell to lighttpd that HTTPS is being used as protocol instead of HTTP. Here is my Nginx confuguration.
server {
server_name mydomain.com;
merge_slashes off;
rewrite ^(.*?)//+(.*?)$ $1/$2 permanent;
location / {
proxy_pass http://localhost:8088/;
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 https;
proxy_set_header X-Forwarded-Ssl on;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
# SSL settings
}
server {
if ($host = mydomain.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
}
The lighttpd server is running a python application that uses web.py module but the returned value by web.ctx.protocol still is HTTP when it should be HTTPS. It looks like lighttpd ignores the X-Forwarded-Proto header sent by Nginx.
What am I doing wrong? Is there any additional configuration to be done?
Thanks.
You have to configure lighttpd to trust headers from upstream. Use mod_extforward in lighttpd. See https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModExtForward
Better than your many headers above, both nginx and lighttpd (via mod_extforward) support RFC 7239 Forwarded header.
https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/
Use of the "Forwarded" header should be preferred.

How to configure tomcat into two different domains in one application?

I wanted to split my applications so that I can configure Tomkat into two different domains "example.com" and "api.example.com". Is it possible to do this with one application? So that some requests are processed on subdomain.
Sure, you can multiple domains of redirecting to multiple Tomcat applications. You would need to install the applications in your Tomcat and make them listen for different ports. Then if you were to use Nginx, all you have to do is change the server clause in your /sites-enabled/ directory from your installation. For your case, you will need two files, one for each domain.
FILE 1
server {
server_name example.com;
location / {
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;
proxy_pass http://localhost:8080;
}
}
FILE 2
server {
server_name api.example.com;
location / {
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;
proxy_pass http://localhost:8081;
}
}
You can find more information in the following url.

nginx prod setup for Clojure WebSocket app

I'm trying to deploy my first Clojure WebSocket app and I think I'm getting close. I get a good response locally, and it looks like the endpoint wants to face the outside world (I see that the port is open when I run netstat), but no response. I'm certain that I have something setup incorrectly in nginx.
I currently already host a few other websites on this server, just want to add the necessary config to get requests made to wss://domain.com:8001 to communicate with my app.
Here is the location entry I'm using now:
location / {
proxy_pass http://localhost:8001;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
access_log /var/www/logs/test.access.log;
error_log /var/www/logs/test.error.log;
}
Could anyone help point me in the right direction? My guess is that I actually have too much in the config, and what's there is probably not correct.
** EDIT: ** For interested parties, I put up my working config (based on Erik Dannenberg's answer) in a gist.
You are missing two more headers, a minimal working config:
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
# add the two below
proxy_set_header Upgrade websocket;
proxy_set_header Connection upgrade;
# optional, but helpful if you run into timeouts
proxy_read_timeout 86400;
}

Generate ssl-certificate and configure RStudio server?

Actually i need to run rstudio server using https.
By default is http://server-ip:8787
I am following this file- (ssl- configuration)
https://s3.amazonaws.com/rstudio-server/rstudio-server-pro-0.98.507-admin-guide.pdf
You can set-up access to the RStudio server via a proxy. By doing that and setting up the Apache or Nginx web server to use SSL, you will have secure access to the RStudio server.
Here's an example of how you can both Shiny and RStudio running on the same domain using SSL and Nginx. If you use https://YOURDOMAIN/ it will run your shiny apps; https://YOURDOMAIN/rstudio to be able to edit the shiny apps directly from the browser!
Replace YOURDOMAIN with your server URL:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
#Server with proxy
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/YOURDOMAIN/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/YOURDOMAIN/privkey.pem;
server_name YOURDOMAIN;
location / {
proxy_pass http://localhost:3838;
proxy_redirect http://localhost:3838/ $scheme://$host/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
}
location /rstudio/ {
rewrite ^/rstudio/(.*)$ /$1 break;
proxy_pass http://localhost:8787;
proxy_redirect http://localhost:8787/ $scheme://$host/rstudio/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
}
}
Unfortunately SSL is only available in the paid version.
See: https://www.rstudio.com/products/rstudio-server-pro/

Resources