Nginx proxy pass to Rails API

2019-09-20 09:26发布

问题:

I have an AngularJS app running in a Nginx server. I´m trying to proxy the requests to a Rails API running in Puma.

I would like all the requests to the api like: http://127.0.0.1/api/getTranslationLanguages go to API server (Puma) listening in the unix socket.

and my Nginx config:

upstream api.development {
    # Path to Puma SOCK file, as defined previously
    server unix:/tmp/puma.sock fail_timeout=0;
}

server {
    listen       80;
    server_name  localhost;        

    access_log  logs/host.access.log;
    rewrite_log on;

     location / {
        root   /path-to-app;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html =404;
     }      

     location /api {          
       proxy_pass http://api.development;
     } 
...
}

At the moment I´m getting a 404 error in the request. Error log:

Request URL:http://127.0.0.1/api/getTranslationLanguages
Request Method:GET
Status Code:404 Not Found
Remote Address:127.0.0.1:80
Referrer Policy:no-referrer-when-downgrade

回答1:

It just worked with:

location /api {        
           rewrite ^/api(.*) /$1 break;
           proxy_pass http://api.development;
        }


标签: nginx proxy