简体   繁体   English

Nginx使用重写反向代理Node.js

[英]Nginx Reverse Proxying to Node.js with Rewrite

I have several apps running behind an Nginx reverse proxy, one of which is a Node server with Express.js. 我在Nginx反向代理后面运行了几个应用程序,其中一个是带有Express.js的节点服务器。 I'm proxying domain.com/demo/app/<path> to localhost:7003/<path> using this Nginx config: 我使用此Nginx配置将domain.com/demo/app/<path>代理到localhost:7003/<path>

http {

    ...

    server {

        listen 80;
        server_name domain.com;

        ...

        location /demo/app {

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Scheme $scheme;

            rewrite ^/demo/app/?(.*) /$1 break;
            proxy_pass http://localhost:7003;
        }

        ...
    }
}

This works great and app receives requests as if it was rooted at / . 这很有效, app接收请求,好像它是以/为根。 The problem is app handles its own static files and might make requests for routes such as css/app.css or images/image.jpg . 问题是app处理自己的静态文件,并可能会对css/app.cssimages/image.jpg这样的路径发出请求。 But because of the reverse proxy, these actually exist at /demo/app/css/app.css and /demo/app/images/image.jpg respectively. 但由于反向代理,它们实际上分别存在于/demo/app/css/app.css/demo/app/images/image.jpg

I've solved this by getting Nginx to pass to Node a custom header indicating the root path, which the Node server prepends to the URLs of all subsequent requests. 我已经通过让Nginx向Node传递一个自定义标头来解决这个问题,该标头指示根路径,节点服务器将其添加到所有后续请求的URL之前。 But now my code is littered with these root path strings. 但现在我的代码充满了这些根路径字符串。 For example, part of my back-end templates: 例如,我的后端模板的一部分:

link(rel='stylesheet', href="#{basePath}/css/base.css")
link(rel='stylesheet', href="#{basePath}/css/skeleton.css")
link(rel='stylesheet', href="#{basePath}/css/layout.css")

What's a more elegant way to handle this? 处理这个问题的更优雅的方法是什么? Isn't there a way to get Nginx to recognize requests coming from an upstream server and automatically forward them to that server? 难道没有办法让Nginx识别来自上游服务器的请求并自动将它们转发到该服务器吗?

I have made nginx serve static files without even passing those requests to node by adding location directive to the app's nginx configuration file (which is included in nginx.conf): 我已经使nginx服务静态文件,甚至没有将这些请求传递给节点,方法是将app指令添加到应用程序的nginx配置文件(包含在nginx.conf中):

location ~ /(img|js)/ {
    rewrite ^(.*)$ /public/$1 break;
}

location / {
    proxy_pass http://localhost:3000/;
    ...
}

In case request comes to /img or /js directory nginx serves files from /public/img or /public/js directory respectively. 如果请求到/ img或/ js目录,nginx分别从/ public / img或/ public / js目录提供文件。 All other requests are proxied to node. 所有其他请求都代理到节点。

You can add more directories if you need (like /css or /views, if you store templates there that you want to use both in node and in browser) and have any directory structure inside those directories, nginx just prepends /public to them and gets files from there without your node app even knowing about it. 如果需要,可以添加更多目录(比如/ css或/ views,如果你在那里存储你想在节点和浏览器中使用的模板),并且在这些目录中有任何目录结构,nginx只是prepends / public到它们和从那里获取文件,而您的节点应用程序甚至不知道它。

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

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