2016-04-15 8 views
1

Ich versuche, die Nginx zu einem separaten Unterverzeichnis für Rails-5-API-Backend zu konfigurieren. (um Frontend und Backend zu trennen)Unterverzeichnis in nginx für Schienen api-app

Original, ich rufe backend api unter GET "/ bills" an. Jetzt möchte ich es sein: GET '/ api/bills'. Daher sollten alle Anfragen unter 'api' an die App 'rails' weitergeleitet werden.

Aber ich kann es nicht funktionieren. Die Umleitung funktioniert, aber ich sehe auf Schienen Seite in den Protokollen: ActionController::RoutingError (No route matches [GET] "/api/bills"). Natürlich existiert diese Route nicht. Rails kennt nur die Route "/ bills". Könnte ich nginx so konfigurieren, dass die Umleitung für Rails transparent ist und die Anfrage wie [GET] "/ bills" angezeigt wird?

hier ist meine aktuelle config:

upstream app { 
    # Path to Unicorn SOCK file, as defined previously 
    server unix:/var/sockets/unicorn.myapp.sock fail_timeout=0; 
} 

server { 
    #redirect to https  
    listen 0.0.0.0:80; 
    listen [::]:80 ipv6only=on default_server; 
    server_name localhost; ## Replace this with something like gitlab.example.com 
    server_tokens off; ## Don't show the nginx version number, a security best practice 
    return 301 https://$server_name$request_uri; 
    access_log /var/log/nginx/app_access.log; 
    error_log /var/log/nginx/app_error.log; 
} 


server { 

    listen 0.0.0.0:443 ssl; 
    listen [::]:443 ipv6only=on ssl default_server; 
    server_name localhost; ## Replace this with something like gitlab.example.com 

    ssl on; 
    ssl_certificate /etc/ssl/nginx/host.crt; 
    ssl_certificate_key /etc/ssl/nginx/host.key; 

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_prefer_server_ciphers on; 
    ssl_session_cache shared:SSL:10m; 
    ssl_session_timeout 5m; 

    location ^~ /api { 
     #try_files $uri/index.html $uri $uri/; 
     try_files $uri/index.html $uri @app; 
     root /app/backend/public; 
    } 


    location @app { 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 
     proxy_pass http://app; 

     error_page 500 502 503 504 /500.html; 
    } 


    location/{ 
     # Application Frontend root, as defined previously 
     try_files $uri $uri/ =404; 
     root /app/frontend/; 
    } 


    client_max_body_size 4G; 
    keepalive_timeout 10; 
} 

Antwort

1

In Ihrem location @app Block versuchen Sie dies:

rewrite ^/api(.*)$ $1 break; 

Das ist nur die /api Präfix abstreifen sollten vor dem stromaufwärts den Rest des URI sendet.

Weitere Informationen finden Sie unter this document.

Verwandte Themen