使用nginx作为反向代理,在Node/Angular/Express应用程序的OPTIONS头上拒绝连接



我正试图让一个express应用程序在Digital Ocean Ubuntu 12.04 Drop上运行,并使用nginx作为反向代理,但当我尝试发布表单时,总是会遇到OPTIONS net::ERR_CONNECTION_REFUSED错误。我尝试了很多不同的nginx配置和cors设置,但仍然会发生这种情况。

使用Node v0.12、Express v4和最新的nginx。

server {
    listen 80;
    server_name thepalette.tk;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
     }
}

编辑:

我找到了nginx错误日志,上面写着:

2015/03/05 05:45:15 [error] 6079#0: *1 connect() failed (111: Connection refused)
while connecting to upstream, 
client: x.x.x.x, 
server: thepalette.tk, 
request: "GET /register HTTP/1.1", 
upstream: "http://x.x.x.x:3000/register", 
host: "thepalette.tk"

在您的app.js中使用cors中间件进行express。例如:

var cors = require("cors");
app.use(cors({
    origin: true,
    credentials: true
}));

相关内容

最新更新