简体   繁体   English

无法配置 nginx 位置(使用重写)?

[英]Can't configure nginx location(use rewrite)?

I have a server running nginx, which is accessible to the Internet through port 80. Let's say the external IP address of my server is 8.41.37.217, and I have a domain srv.vvzvlad.xyz, which points to this address.我有一台运行 nginx 的服务器,可以通过端口 80 访问 Internet。假设我的服务器的外部 IP 地址是 8.41.37.217,我有一个域 srv.vvzvlad.xyz,它指向这个So when I open h[tt]p://srv.vvzvlad.xyz in my browser, I see the nginx welcome page.因此,当我在浏览器中打开 h[tt]p://srv.vvzvlad.xyz 时,我会看到 nginx 欢迎页面。 So far everything is working.到目前为止,一切正常。

I have docker running on the server with some containers that implement web applications.我在服务器上运行了 docker,其中一些容器实现了 web 应用程序。 These applications inside the docker containers open ports 80, and outside the containers (from the internal network) these ports are available as 3333 or 4444 or similar. docker 容器内部的这些应用程序打开端口 80,而容器外部(来自内部网络)这些端口可用作 3333 或 4444 或类似端口。 So, by opening the local server address (192.168.88.111) in the browser with port 3333, I access the port of the docker container where the wiki is running: h[tt]p://192.168.88.111:3333 redirects to 10.0.0.2:80, where 10.0.0.2 is the internal address of the docker container.因此,通过在浏览器中使用端口 3333 打开本地服务器地址(192.168.88.111),我访问了运行 wiki 的 docker 容器的端口:h[tt]p://192.168.88.111:3333 重定向到 10.0 .0.2:80,其中 10.0.0.2 是 docker 容器的内部地址。 This also works.这也有效。

Now I want to access these docker containers from the internet.现在我想从 Internet 访问这些 docker 容器。 I could just open ports on the router, and go to addresses like h[tt]p://srv.vvzvlad.xyz:3333, but some software that will send requests doesn't know how to handle ports other than 80/443.我可以只打开路由器上的端口,并打开 go 到 h[tt]p://srv.vvzvlad.xyz:3333 之类的地址,但是一些发送请求的软件不知道如何处理 80/443 以外的端口. That's why I set up some locations on nginx (see the first paragraph) which redirect URLs like h[tt]p://srv.vvzvlad.xyz(:80)/wiki to h[tt]p://192.168.88.111:3333.这就是为什么我在 nginx 上设置了一些位置(见第一段),这些位置将诸如 h[tt]p://srv.vvzvlad.xyz(:80)/wiki 之类的 URL 重定向到 h[tt]p://192.168.88.111 :3333。

I do it like this:我这样做:

server {
    listen 80;
...
    location /wiki {
        proxy_pass h[tt]p://192.168.88.111:3333;
    }
...
}

My network structure, which I just described, looks something like this: https://habrastorage.org/webt/ud/va/8p/udva8p6ggl_ojb3cde-yessdnmk.png我刚才描述的网络结构看起来像这样: https://habrastorage.org/webt/ud/va/8p/udva8p6ggl_ojb3cde-yessdnmk.png

This scheme works, but is added to any address inside the application "/wiki".此方案有效,但添加到应用程序“/wiki”内的任何地址。 Thus, the address of the application is h[tt]p://192.168.88.111:3333/index in h[tt]p://srv.vvzvlad.xyz/wiki/index.因此,应用程序的地址是 h[tt]p://srv.vvzvlad.xyz/wiki/index 中的 h[tt]p://192.168.88.111:3333/index。 With some applications this works fine.对于某些应用程序,这可以正常工作。 But some applications expect to run exclusively at the root of the web server.但有些应用程序希望在 web 服务器的根目录中独占运行。 They work fine with the address h[tt]p://srv.vvzvlad.xyz directly (h[tt]p://srv.vvzvlad.xyz/index), but cannot work with the added address (h[tt]p://srv.vvzvlad.xyz/wiki/index), because they still use internally absolute links to their resources like h[tt]p://srv.vvzvlad.xyz/favicon.png, which of course are not on "location /", but only on "location /wiki".它们可以直接使用地址 h[tt]p://srv.vvzvlad.xyz (h[tt]p://srv.vvzvlad.xyz/index),但不能使用添加的地址 (h[tt] p://srv.vvzvlad.xyz/wiki/index),因为他们仍然在内部使用指向其资源的绝对链接,例如 h[tt]p://srv.vvzvlad.xyz/favicon.png,这当然不在“位置 /”,但仅限于“位置 /wiki”。

How do I solve this problem?我该如何解决这个问题? I've tried using rewrite(eg "rewrite ^/wiki/(.*)$ h[tt]p://192.168.88.111:3000/$1;"), but the resources the application requests still remain without /wiki: https://habrastorage.org/webt/zj/s4/oa/zjs4oazs7ywo10c_zffxs8pkpnw.png我试过使用重写(例如“rewrite ^/wiki/(.*)$ h[tt]p://192.168.88.111:3000/$1;”),但应用程序请求的资源仍然没有/wiki: https://habrastorage.org/webt/zj/s4/oa/zjs4oazs7ywo10c_zffxs8pkpnw.png

Use a trailing slash for both should help for most.对两者都使用斜杠应该对大多数人都有帮助。 Otherwise play around with the slashes.. it is always a pain..否则玩斜线..总是很痛苦..

 location /wiki/ {
        proxy_pass http://192.168.88.111:3333/;

/wiki is regexing for all terms wiki* /wiki 正在对所有术语 wiki* 进行正则表达式

Also a possibility:还有一种可能:

 location ~* /wiki/(.*)$ {
        proxy_pass http://192.168.88.111:3333/$1;

If you can't reach /wiki (without trailing slash) in browser after that:如果之后您无法在浏览器中访问 /wiki(没有斜杠):

 location /wiki {
        return 308 http://$host/wiki/

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

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