explicitly handle specific path #53

Open
vkuznet opened this Issue Aug 23, 2012 · 7 comments

Projects

None yet

3 participants

@vkuznet
vkuznet commented Aug 23, 2012

Hi,
I'm new to nginx, so my question may seems silly. Anyway, I'm trying to come up with configuration of nginx which will allow proxy to websocket server only for specific path, e.g. /websocket, while preserve all other paths (including default one /) intact and handled by http nginx configuration. How can I do that? I found that I can explicitly add new location in http section, but default one is still redirected to tcp proxy. In other words, right now the the proxy redirection is done implicitly, while I'd like to have explicit redirection.
Thanks,
Valentin.

@yaoweibin
Owner

On 2012/8/24 2:49, Valentin Kuznetsov wrote:

Hi,
I'm new to nginx, so my question may seems silly. Anyway, I'm trying
to come up with configuration of nginx which will allow proxy to
websocket server only for specific path, e.g. /websocket, while
preserve all other paths (including default one /) intact and handled
by http nginx configuration. How can I do that? I found that I can
explicitly add new location in http section, but default one is still
redirected to tcp proxy. In other words, right now the the proxy
redirection is done implicitly, while I'd like to have explicit
redirection.

What's the meaning of implicitly redirection? Can you show me your
config? Does that work?

Thanks,
Valentin.


Reply to this email directly or view it on GitHub
#53.

Thanks
-YWB

@vkuznet
vkuznet commented Aug 24, 2012

Hi,
here is my scenario. I have multiple back-ends hosting different applications on different ports. Moreover a single server can host both websockets and HTTP applications. It can be configured in way that
http://app_host:port/ will host HTTP application and http://app_host:port/websocket will host WebSocket one. Therefore my proxy server should be capable to redirect properly different paths. For instance,
https://proxy_server/ will serve entry page
https://proxy_server/static will host static pages
https://proxy_server/websocket will redirect to app_host:port/websocket
https://proxy_server/path will redirect to app_host:port/
etc.

Here is nginx.conf I experiment with:

worker_processes 1;

events {
worker_connections 1024;
}

tcp proxy to remote server via HTTPS

tcp {
upstream websockets {
server 127.0.0.1:9000;
check interval=3000 rise=2 fall=5 timeout=1000;
}
server {
server_name _;
listen 127.0.0.1:443;
ssl on;
ssl_certificate /path/conf/server.crt;
ssl_certificate_key /path/conf/server.key;
so_keepalive on;
tcp_nodelay on;
proxy_pass websockets;
}
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
    listen              80;
    server_name         localhost;
    rewrite ^/(.*) https://localhost/$1 permanent;

    location / {
        root   html;
        index  index.html index.htm;
    }
}


# HTTPS server
server {
    listen       443;
    server_name  localhost;
    ssl                  on;
    ssl_certificate      /path/conf/server.crt;
    ssl_certificate_key  /path/conf/server.key;
    location / {
        root   html;
        index  index.html index.htm;
    }
    location /app {
        proxy_pass      http://localhost:9000/app/;
        proxy_redirect  off;
    }
    location /websocket {
        root   html;
        index  index.html index.htm;
    }
}

}

Please note that in last section of HTTPS server I experiment with location directive (obviously I don't know yet how to properly configure it, that's why I ask the question). Anyway, my back-end server runs on localhost:9000 and it has both /websocket (WebSocket) and /app (HTTP) apps.
Thanks,
Valentin.

@yaoweibin
Owner

Thanks. But I'm afraid you can't use the same port with HTTP and tcp
proxy. It will not work.

On 2012-8-24 21:21, Valentin Kuznetsov wrote:

Hi,
here is my scenario. I have multiple back-ends hosting different
applications on different ports. Moreover a single server can host
both websockets and HTTP applications. It can be configured in way that
http://app_host:port/ will host HTTP application and
http://app_host:port/websocket will host WebSocket one. Therefore my
proxy server should be capable to redirect properly different paths.
For instance,
https://proxy_server/ will serve entry page
https://proxy_server/static will host static pages
https://proxy_server/websocket will redirect to app_host:port/websocket
https://proxy_server/path will redirect to app_host:port/
etc.

Here is nginx.conf I experiment with:

worker_processes 1;

events {
worker_connections 1024;
}

tcp proxy to remote server via HTTPS

tcp {
upstream websockets {
server 127.0.0.1:9000;
check interval=3000 rise=2 fall=5 timeout=1000;
}
server {
server_name _;
listen 127.0.0.1:443;
ssl on;
ssl_certificate /path/conf/server.crt;
ssl_certificate_key /path/conf/server.key;
so_keepalive on;
tcp_nodelay on;
proxy_pass websockets;
}
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;
rewrite ^/(.*) https://localhost/$1 permanent;

location / {
root html;
index index.html index.htm;
}
}

HTTPS server

server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate /path/conf/server.crt;
ssl_certificate_key /path/conf/server.key;
location / {
root html;
index index.html index.htm;
}
location /app {
proxy_pass http://localhost:9000/app/;
proxy_redirect off;
}
location /websocket {
root html;
index index.html index.htm;
}
}
}

Please note that in last section of HTTPS server I experiment with
location directive (obviously I don't know yet how to properly
configure it, that's why I ask the question). Anyway, my back-end
server runs on localhost:9000 and it has both /websocket (WebSocket)
and /app (HTTP) apps.
Thanks,
Valentin.


Reply to this email directly or view it on GitHub
#53 (comment).

Thanks,
-Weibin Yao

@vkuznet
vkuznet commented Aug 24, 2012

Is it technically impossible?

Are you saying that I need to configure nginx to use different ports, where one will serve HTTP traffic and another WebSocket? Something like:
http requests will go to 443 port
WebSocket requests will go to 8433 port

Usually on production system people don't have much choice and are given just single access point (http and/or https access, therefore a single port). So the adoption of technology can be problematic.

On Aug 24, 2012, at ,Aug 24, 11:36 AM, Weibin Yao(姚伟斌) wrote:

Thanks. But I'm afraid you can't use the same port with HTTP and tcp
proxy. It will not work.

On 2012-8-24 21:21, Valentin Kuznetsov wrote:

Hi,
here is my scenario. I have multiple back-ends hosting different
applications on different ports. Moreover a single server can host
both websockets and HTTP applications. It can be configured in way that
http://app_host:port/ will host HTTP application and
http://app_host:port/websocket will host WebSocket one. Therefore my
proxy server should be capable to redirect properly different paths.
For instance,
https://proxy_server/ will serve entry page
https://proxy_server/static will host static pages
https://proxy_server/websocket will redirect to app_host:port/websocket
https://proxy_server/path will redirect to app_host:port/
etc.

Here is nginx.conf I experiment with:

worker_processes 1;

events {
worker_connections 1024;
}

tcp proxy to remote server via HTTPS

tcp {
upstream websockets {
server 127.0.0.1:9000;
check interval=3000 rise=2 fall=5 timeout=1000;
}
server {
server_name _;
listen 127.0.0.1:443;
ssl on;
ssl_certificate /path/conf/server.crt;
ssl_certificate_key /path/conf/server.key;
so_keepalive on;
tcp_nodelay on;
proxy_pass websockets;
}
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;
rewrite ^/(.*) https://localhost/$1 permanent;

location / {
root html;
index index.html index.htm;
}
}

HTTPS server

server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate /path/conf/server.crt;
ssl_certificate_key /path/conf/server.key;
location / {
root html;
index index.html index.htm;
}
location /app {
proxy_pass http://localhost:9000/app/;
proxy_redirect off;
}
location /websocket {
root html;
index index.html index.htm;
}
}
}

Please note that in last section of HTTPS server I experiment with
location directive (obviously I don't know yet how to properly
configure it, that's why I ask the question). Anyway, my back-end
server runs on localhost:9000 and it has both /websocket (WebSocket)
and /app (HTTP) apps.
Thanks,
Valentin.


Reply to this email directly or view it on GitHub
#53 (comment).

Thanks,
-Weibin Yao

Reply to this email directly or view it on GitHub.

@yaoweibin
Owner

No, It's not supported yet. Otherwise you can just use the tcp proxy
module purely, but its feature with HTTP proxy is very limited
(https://github.com/yaoweibin/nginx_tcp_proxy_module/wiki/websocket).

On 2012-8-25 0:02, Valentin Kuznetsov wrote:

Is it technically impossible?

Are you saying that I need to configure nginx to use different ports,
where one will serve HTTP traffic and another WebSocket? Something like:
http requests will go to 443 port
WebSocket requests will go to 8433 port

Usually on production system people don't have much choice and are
given just single access point (http and/or https access, therefore a
single port). So the adoption of technology can be problematic.

On Aug 24, 2012, at ,Aug 24, 11:36 AM, Weibin Yao(姚伟斌) wrote:

Thanks. But I'm afraid you can't use the same port with HTTP and tcp
proxy. It will not work.

On 2012-8-24 21:21, Valentin Kuznetsov wrote:

Hi,
here is my scenario. I have multiple back-ends hosting different
applications on different ports. Moreover a single server can host
both websockets and HTTP applications. It can be configured in way
that
http://app_host:port/ will host HTTP application and
http://app_host:port/websocket will host WebSocket one. Therefore my
proxy server should be capable to redirect properly different paths.
For instance,
https://proxy_server/ will serve entry page
https://proxy_server/static will host static pages
https://proxy_server/websocket will redirect to
app_host:port/websocket
https://proxy_server/path will redirect to app_host:port/
etc.

Here is nginx.conf I experiment with:

worker_processes 1;

events {
worker_connections 1024;
}

tcp proxy to remote server via HTTPS

tcp {
upstream websockets {
server 127.0.0.1:9000;
check interval=3000 rise=2 fall=5 timeout=1000;
}
server {
server_name _;
listen 127.0.0.1:443;
ssl on;
ssl_certificate /path/conf/server.crt;
ssl_certificate_key /path/conf/server.key;
so_keepalive on;
tcp_nodelay on;
proxy_pass websockets;
}
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;
rewrite ^/(.*) https://localhost/$1 permanent;

location / {
root html;
index index.html index.htm;
}
}

HTTPS server

server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate /path/conf/server.crt;
ssl_certificate_key /path/conf/server.key;
location / {
root html;
index index.html index.htm;
}
location /app {
proxy_pass http://localhost:9000/app/;
proxy_redirect off;
}
location /websocket {
root html;
index index.html index.htm;
}
}
}

Please note that in last section of HTTPS server I experiment with
location directive (obviously I don't know yet how to properly
configure it, that's why I ask the question). Anyway, my back-end
server runs on localhost:9000 and it has both /websocket (WebSocket)
and /app (HTTP) apps.
Thanks,
Valentin.


Reply to this email directly or view it on GitHub

#53 (comment).

Thanks,
-Weibin Yao

Reply to this email directly or view it on GitHub.


Reply to this email directly or view it on GitHub
#53 (comment).

Thanks,
-Weibin Yao

@machowski

Hello,
I think I've got similar problem.

I've got application running on tomcat that is handing paths
localhost:8080 - regular http page
localhost:8080/websocket - websocket connection

I want to set up ngix to handle both paths
mydomain.com and mydomain.com/websocket

As far as I understand from this topic it is not possible right now? Is there any plan to support such feature? Any workaround?

@yaoweibin
Owner

As the official Nginx team will add this feature in the first season of 2013 (http://trac.nginx.org/nginx/roadmap ). This modification need change the nginx core code a lot. I don't want to do it in my end. Let's wait for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment