简体   繁体   English

Nginx 位置和子位置块

[英]Nginx location & sub location blocks

I'm not very experienced with nginx but I know enough to get by.我对 nginx 不是很有经验,但我知道足够了。

Recently I came across a bug that is now resolved, but I wanted clarification as to why this would be occuring.最近我遇到了一个错误,现在已经解决了,但我想澄清为什么会发生这种情况。

So I have several node applications and nginx works as a reverse proxy towards them.所以我有几个节点应用程序,nginx 作为它们的反向代理。 The webserver is setup with HTTPS and one of these applications has a login page with a mongoDB backend that it uses to persist sessions. Web 服务器设置为 HTTPS,其中一个应用程序有一个登录页面,后端为 mongoDB,用于保持会话。

The location block for this application is this:此应用程序的位置块是这样的:

location /app/abcd {
    proxy_pass http://localhost:3003;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

I also had another application which had a similar location block but it was set to point towards the root like so:我还有另一个具有类似位置块的应用程序,但它被设置为指向根,如下所示:

location / {
    proxy_pass http://localhost:3002;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

My goal was if you went to app/abcd then the first app would handle the request, and anything else would be handled by the second one.我的目标是,如果您转到 app/abcd,那么第一个应用程序将处理请求,而其他任何内容都将由第二个应用程序处理。

Everything seemed to be working fine except the persistence of sessions.除了会话的持久性之外,一切似乎都运行良好。 If I tried to goto /app/abcd it would store the sessions fine but it seemed that users were losing ownership of their sessions upon every page refresh.如果我尝试转到 /app/abcd,它会很好地存储会话,但似乎用户在每次刷新页面时都失去了对会话的所有权。

My NodeJS session code looked like this:我的 NodeJS session 代码如下所示:

app.set('trust proxy', 1);

app.use(session({
    secret: 'secret',
    resave: false,
    saveUninitialized: true,
    proxy: true,
    cookie: {
        httpOnly: false,
        expires: false,
        maxAge: 1000000000000,
        secure: true
    },
    store: new MongoStore({
        mongooseConnection: db
    })
}));

Once I removed the block that was pointing towards location / it all started working.一旦我删除了指向位置的块/它就开始工作了。 I don't know enough about nginx to understand what it was doing behind the scenes to break session persistence, could somebody explain to me what was happening?我对 nginx 了解不够,无法理解它在幕后做了什么来打破 session 持久性,有人可以向我解释发生了什么吗?

What is happening here is that Nginx will pass the full path towards your Express application (NodeJS), in your case this would be /app/abcd .这里发生的是 Nginx 将传递到您的 Express 应用程序 (NodeJS) 的完整路径,在您的情况下这将是/app/abcd

However, your Express application has routes listening on only / .但是,您的 Express 应用程序仅在/上侦听路由。 Let's say your backend code has the following JavaScript, which results in a 404 not found:假设您的后端代码具有以下 JavaScript,这会导致 404 not found:

app.use('/', routes)

You could change your backend code (NodeJs) to match the add path, for example:您可以更改后端代码 (NodeJs) 以匹配添加路径,例如:

app.use('/app/abcd', routes)

OR alternatively , make Nginx strip the app/abcd section from the request.或者,使 Nginx 从请求中删除app/abcd部分。 In this latter case you just need to add a trailing slash to the end of the proxy_pass section (note the extra / at the end):在后一种情况下,您只需要在proxy_pass部分的末尾添加一个尾部斜杠(注意末尾的额外/ ):

proxy_pass http://localhost:3002/;

See also: https://serverfault.com/a/601366/227596另请参阅: https://serverfault.com/a/601366/227596

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM