2016-04-15 3 views
1

rails-5 apiバックエンド用の別のサブディレクトリを持つようにnginxを設定しようとしています。 (フロントエンドとバックエンドを分離する)レールのためのnginxのsubdir api-app

オリジナル、私はGET "/ bill"の下でバックエンドapiを呼び出しています。今私はそれになりたい:GET '/ api/bills'。したがって、 'api'の下のすべてのリクエストは、rails appにリダイレクトされる必要があります。

しかし、私はそれを働かせることはできません。リダイレクトは機能しますが、ログのレール側にはActionController::RoutingError (No route matches [GET] "/api/bills")があります。もちろん、このルートは存在しません。 Railsは "/ bill"のルートしか知りません。リダイレクトがRailsに透過的で、[GET] "/ bill"のようなリクエストが表示されるように、nginxを設定できますか?これを追加あなたのlocation @appブロック試しインサイド

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; 
} 

答えて

1

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

すぐ上流URIの残りの部分を送信する前に/apiプレフィックスを取り除く必要があります

は、ここに私の現在の設定です。

詳細はthis documentを参照してください。

関連する問題