nginx - 如何在Nginx中重写或代理URL?

标签 nginx docker

我是nginx的新手。我想改写

feature-1234.mydomain.com/xyz?foo=bar


docker-feature-1234:9000/xyz?foo=bar

与请求参数。

我使用官方的nginx docker镜像。我的nginx.conf文件应该如何?

编辑:'feature-1234'是一个变量,所以:

feature-5678.mydomain.com

应该作为

docker-feature-5678:9000

最佳答案

通过结合答案,我找到了解决方案。

https://serverfault.com/questions/388552/nginx-sub-domain-proxy-pass

Docker Network Nginx Resolver

nginx: [emerg] "server" directive is not allowed here

nginx.conf:

events {
}

http {
  server {
    listen 80;
    server_name ~(.*).test.go;

    location / {
      if ($host ~* ^([a-zA-Z0-9-]+)\.test\.go$) {
        set $proxyhost docker-$1:9000;
      }

      resolver 127.0.0.11 ipv6=off;
      proxy_pass http://$proxyhost;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;

    }
  }
}

谢谢

关于nginx - 如何在Nginx中重写或代理URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322153/

相关文章:

docker - 如何使用 nsenter 实用程序从容器连接主机

docker - 在docker容器中启用ipv6转发

bash - 将当前环境变量复制到 `docker run`的范围

node.js - 带有 SSL 的 NodeJS Express 不适用于 Nginx 反向代理

http - MantisBT 图片预览大小设置

regex - Nginx 使用特殊字符和正则表达式重写规则

node.js - NGINX 未正确设置 X-Forwarded-For

ubuntu - ubuntu 上安装 nginx

Docker M1 - 使用 Ubuntu + MySQL :/bin/sh errors on latest update Docker Desktop 4. 2.0

mysql - 无法从 docker 容器连接到本地主机 Mysql