Ad

Nginx Proxy Configuration With Path Rewrite

I made an angular app which communicate to a nodeJS server. I finally achieved to configure the proxy of the dev web server of angular with the following configuration file and everything works very fine :

{  
    "/api/*": 
    {    
        "target": "http://localhost:8080",    
        "secure": false,    
        "pathRewrite": {"^/api" : ""}
    },
    "/sock/*": 
    {
        "target": "http://localhost:8060/socket.io/"
    }
}

Then I tried to deploy my application with a nginx web server and to configure the server and the proxy with the following configuration :

server {
        listen 3600;
        listen [::]:3600;

        server_name localhost;

        root /home/ubuntu/mahe/tchernobyl/angular_tchernobyl/dist/tchernobyl;
        index index.html index.htm index.nginx-debian.html;

        location / {
                try_files $uri $uri/ /index.html;
        }

        location /sock {
                proxy_pass http://localhost:8060/socket.io;
        }

        location /api {
                   proxy_pass http://localhost:8080;
                   sub_filter /api/ /;
        }
}

Somehow it finally appears to work with the socket.io, I just have one error message at each refresh I don't know why :

WebSocket connection to 'ws://localhost:8100/sock/?EIO=3&transport=websocket&sid=qU-TS9vQwIPwZej7AAAK' failed: Error during WebSocket handshake: Unexpected response code: 400

EDIT: This very secondary problem was resolved by applying this solution https://github.com/socketio/socket.io/issues/1942#issuecomment-82352072


But the main problem is that, I can totally reach my express server serving data via /api/[key] but I cannot get any data. To explain : my express server is running on 8080 and the web on 3600. If I type http://localhost:8080/data, I get my data but if I type http://localhost:3600/api/data I get 'Cannot GET /data' response. (notice that I rewrite with 'sub_filter /api/ /;')

I get the following message in the console :

Refused to execute a script because its hash, its nonce, or 'unsafe-inline' appears in neither the script-src directive nor the default-src directive of the Content Security Policy.
Ad

Answer

This was "sub_filter /api/ /;" that didn't rewrite the URL as I thought, I replaced it by

rewrite /foo/(.*) /$1 break;

and it finally worked.

In addition to this adjustment that can be found in the initial edited message : https://github.com/socketio/socket.io/issues/1942#issuecomment-82352072

Ad
source: stackoverflow.com
Ad