我正在尝试访问在Sails
上运行的pm2
应用。如果我直接从运行它的服务器的IP访问它,应用程序就会运行。
当我在另一台服务器上运行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 :(得分:0)
我用以下配置解决了这个问题:
由于服务直接在Node/PM2
上运行,如果我们在位置路径之后使用/
,则不会转发子路径。
因此,对于没有Nginx运行的应用程序,我们使用location /auth/
代替location /auth
。
我们还需要转发标题和数据。
如果转发的应用程序在Nginx上运行,则在我们使用location /auth/
时转发路径,即调用的服务将是http://myserver.com:1337/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';
}