2017-08-11 239 views
0

我試圖訪問運行在pm2上的Sails應用程序。如果我直接從運行它的服務器的IP訪問應用程序,則應用程序運行。Nginx上的反向代理服務器從一臺服務器到另一臺

當我在另一臺服務器上運行的Nginx作爲反向代理,下面的情況:

當我使用上origin.myserver.com下面的配置,它仍然有效:

server { 
listen 80; 

location/{ 
    client_max_body_size 10m; 
    client_body_buffer_size 128k; 

    proxy_send_timeout 600; 
    proxy_read_timeout 600; 
    proxy_buffer_size 128k; 
    proxy_buffers  4 256k; 
    proxy_busy_buffers_size 256k; 
    proxy_temp_file_write_size 256k; 
    proxy_connect_timeout 30s; 
    proxy_set_header Host $host; 
    proxy_pass http://dest.myserver:1337; 

    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
} 
} 

然而,當我嘗試添加從Nginx的多個子目錄,這是行不通的:

server { 
listen 80; 

location /auth { 
    client_max_body_size 10m; 
    client_body_buffer_size 128k; 

    proxy_send_timeout 600; 
    proxy_read_timeout 600; 
    proxy_buffer_size 128k; 
    proxy_buffers  4 256k; 
    proxy_busy_buffers_size 256k; 
    proxy_temp_file_write_size 256k; 
    proxy_connect_timeout 30s; 
    proxy_set_header Host $host; 
    proxy_pass http://dest.myserver:1337; 

    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
} 
} 

我收到的錯誤是:

2017/08/11 15:55:13 [error] 8304#0: *3 open() "/usr/share/nginx/html/locales/locale.js" failed (2: No such file or directory), client: IP, server: , request: "GET /locales/locale.js?v=1502466823352 HTTP/1.1", host: "REVERSE PROXY IP" 

回答

0

我解決了以下配置問題:

由於服務是在Node/PM2直接,子路徑將不被轉發運行,如果我們的位置路徑後使用/。 因此,對於沒有Nginx運行的應用程序,我們使用location /auth/而不是location /auth

而且我們還需要轉發標題和數據。

如果應用程序被轉發到,運行於Nginx的,路徑是,如果我們使用一個稱爲是http://myserver.com:1337/auth

location /auth/即服務轉發我的情況是:

server { 

    listen 80; 

    location /auth/ { 
     proxy_set_header Host origin.myserver.com/auth/; 
     proxy_pass http://dest.myserver.com:1337/; 

     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     client_max_body_size 10m; 
     client_body_buffer_size 128k; 

     proxy_send_timeout 600; 
     proxy_read_timeout 600; 
     proxy_buffer_size 128k; 
     proxy_buffers  4 256k; 
     proxy_busy_buffers_size 256k; 
     proxy_temp_file_write_size 256k; 
     proxy_connect_timeout 300s; 
     add_header 'Access-Control-Allow-Origin' 'http://someother.domain.in'; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
     add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 
} 
相關問題