简体   繁体   English

NGINX + ExpressJS-HTML中URL的反向代理

[英]NGINX + ExpressJS - Reverse proxy for URLs in HTML

I have a NodeJS + ExpressJS app running on port 3000 which has a route: 我有一个运行在端口3000上的NodeJS + ExpressJS应用,该应用具有一条路由:

router.get('/getHello', function(req, res) {
    res.send('Hello, world!');
});

and a HTML page which does a GET on this route 以及在此路线上执行GET的HTML页面

<a href="/getHello">
    <input type="button" value="Visit Helo World page" />
</a>

This standalone app works as intended. 这个独立的应用程序按预期工作。 It displays Hello, world! 它显示Hello, world! when the button is pressed. 当按下按钮时。

Now, here's the problem: 现在,这是问题所在:

I have setup a reverse proxy on this app using nginx . 我已经使用nginx在此应用程序上设置了反向代理。 Here's the config file from sites-available (linked with sites-enabled ) 这是sites-available的配置文件(与sites-enabled链接)

server {
    listen 80;
    server_name localhost;

    location /routetest/ {
        proxy_pass http://127.0.0.1:3000/;
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
        proxy_cache_bypass $http_upgrade;
    }
}

The app opens fine when I visit http://localhost/routetest but when I click on the button, it opens http://localhost/getHello whereas I wanted it to open http://localhost/routetest/getHello . 当我访问http://localhost/routetest时,该应用程序可以正常打开,但是当我单击按钮时,它会打开http://localhost/getHello而我希望它打开http://localhost/routetest/getHello Since http://localhost/getHello is not defined, I end up with a 404. (On the other hand, http://localhost/routetest/getHello correctly returns the Hello, world message) 由于http://localhost/getHello ,因此我得到一个404。(另一方面, http://localhost/routetest/getHello正确返回Hello, world消息)

Now, my question is: 现在,我的问题是:

My frontend is written with / as the base path (to make all GET and POST requests), for example <a href="/getHello"> and I feel manually appending /routetest/ before all URLs in my HTML not a good practice (since I may want to change this base path later and then I'll have to update it everywhere). 我的前端写有/作为基本路径(进行所有GET和POST请求),例如<a href="/getHello"> ,我觉得在HTML中的所有URL之前手动附加 /routetest/并不是一个好习惯(因为稍后可能要更改此基本路径,然后必须在任何地方进行更新)。 So, is there a way for NGINX or some express middleware to add /routetest/ for URLs that are written with / as the base path, without having to change it in my HTML manually? 那么, NGINX或某些express中间件的方法是否可以为使用/作为基本路径编写的URL添加/routetest/ ,而不必手动在HTML中进行更改?

By a long way the easiest solution is to use relative paths without the leading / if that's an option that's available to you. 从长远来看,最简单的解决方案是使用相对路径而不使用前导/如果可以的话)。

Rewriting URLs in the response is somewhere between tricky and impossible in the general case. 在一般情况下,在响应中重写URL介于棘手和不可能之间。 If you think you can successfully identify the relevant URLs in your content you might be able to use nginx's catchily titled ngx_http_sub_module : 如果您认为可以成功识别内容中的相关URL,则可以使用nginx的标题为ngx_http_sub_module

http://nginx.org/en/docs/http/ngx_http_sub_module.html http://nginx.org/en/docs/http/ngx_http_sub_module.html

There are various Express middleware implementations that do a similar thing but I couldn't find one that looked reliable enough to recommend. 有多种Express中间件实现可以做类似的事情,但我找不到足够可靠的建议。 The basic approach would be similar to the compression middleware if you fancy giving it a go yourself. 如果您愿意自己尝试一下,则基本方法将类似于compression中间件。 Again it's quite difficult in general but depending on the specifics of how you serve up your HTML it might not be too bad. 同样,这通常很困难,但是取决于您如何提供HTML的细节,它可能还不错。

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

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