Nginx反向代理Node.js与重写 [英] Nginx Reverse Proxying to Node.js with Rewrite

查看:113
本文介绍了Nginx反向代理Node.js与重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个应用程序运行在Nginx反向代理后面,其中之一是具有Express.js的Node服务器。我代理 domain.com/demo/app/<path> localhost:7003 /< path> 使用这个Nginx配置:

  http {

...

服务器{

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;

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

...
}
}

这样做很好,应用程序收到请求,就像它已经在 / 一样。问题是 app 处理其自己的静态文件,并可能会请求路由,例如 css / app.css 图像/ image.jpg的。但是由于反向代理,这些实际上存在于 /demo/app/css/app.css / demo / app / images / image。 jpg



我已经解决了这个问题,让Nginx传递给Node一个自定义的头部,指示根节点,Node服务器预先添加到所有后续请求的URL。但是现在我的代码散布着这些根路径字符串。例如,我的后端模板的一部分:

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

什么是更优雅的方式来处理?没有办法让Nginx识别来自上游服务器的请求,并自动将其转发到该服务器?

解决方案

我已经使nginx提供静态文件,甚至没有通过向应用程序的nginx配置文件(包括在nginx.conf中)添加位置指令来将节点传递给节点:

 位置〜/(img | js)/ {
重写^(。*)$ / public / $ 1 break;
}

位置/ {
proxy_pass http:// localhost:3000 /;
...
}

如果请求到/ img或/ js目录nginx分别从/ public / img或/ public / js目录提供文件。所有其他请求都被代理到节点。



如果需要,您可以添加更多目录(如/ css或/ views,如果您要在其中存储要使用的模板在节点和浏览器中),并且在这些目录中有任何目录结构,nginx只是向他们添加/公开,并从那里获取文件,而没有你的节点应用程序甚至知道它。


I have several apps running behind an Nginx reverse proxy, one of which is a Node server with Express.js. I'm proxying domain.com/demo/app/<path> to localhost:7003/<path> using this Nginx config:

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 /. The problem is app handles its own static files and might make requests for routes such as css/app.css or images/image.jpg. But because of the reverse proxy, these actually exist at /demo/app/css/app.css and /demo/app/images/image.jpg respectively.

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. 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?

解决方案

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):

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. 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.

这篇关于Nginx反向代理Node.js与重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆