title: Nginx proxy configuration with path rewrite
body: <p>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 :</p>
  
  <pre><code>{  
      "/api/*": 
      {    
          "target": "http://localhost:8080",    
          "secure": false,    
          "pathRewrite": {"^/api" : ""}
      },
      "/sock/*": 
      {
          "target": "http://localhost:8060/socket.io/"
      }
  }
  </code></pre>
  
  <p>Then I tried to deploy my application with a nginx web server and to configure the server and the proxy with the following configuration :</p>
  
  <pre><code>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/ /;
          }
  }
  </code></pre>
  
  <hr>
  
  <p>Somehow it finally appears to work with the socket.io, I just have one error message at each refresh I don't know why :</p>
  
  <pre><code>WebSocket connection to 'ws://localhost:8100/sock/?EIO=3&amp;transport=websocket&amp;sid=qU-TS9vQwIPwZej7AAAK' failed: Error during WebSocket handshake: Unexpected response code: 400
  </code></pre>
  
  <p>EDIT: This very secondary problem was resolved by applying this solution <a href="https://github.com/socketio/socket.io/issues/1942#issuecomment-82352072" rel="nofollow noreferrer">https://github.com/socketio/socket.io/issues/1942#issuecomment-82352072</a></p>
  
  <hr>
  
  <p>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 <a href="http://localhost:8080/data" rel="nofollow noreferrer">http://localhost:8080/data</a>, I get my data but if I type <a href="http://localhost:3600/api/data" rel="nofollow noreferrer">http://localhost:3600/api/data</a> I get 'Cannot GET /data' response. (notice that I rewrite with 'sub_filter /api/ /;')</p>
  
  <p>I get the following message in the console :</p>
  
  <pre><code>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.
  </code></pre>