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


4

我是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

应该作为

码头工人- 功能5678:9000

Answers:


1

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

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

https://stackoverflow.com/questions/35744650/docker-network-nginx-resolver

https://stackoverflow.com/questions/41766195/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;

    }
  }
}

谢谢


0

site.conf

server_tokens关闭;

map $ http_upgrade $ connection_upgrade {默认升级;''
关闭; }

服务器{

设置$ access_log'var / log / nginx / my-site.access.log'; 设置$ error_log'var / log / nginx / my-site.error.log';

听80; server_name feature-1234.mydomain.com;

access_log / $ access_log; error_log / $ error_log;

位置 / {

proxy_pass docker-feature-1234:9000;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;

if ($uri != '/') { expires 30d;}
access_log /$access_log;

} }

贴上

触摸/etc/nginx/sites-available/site.conf

下一个

ln -s /etc/nginx/sites-available/site.conf / etc / nginx / sites-enabled

下一个

服务nginx重新加载

如果您没有root访问权限,则将sudo添加到命令开头


感谢您的回答,但我更新了问题,使其更加清晰。功能名称将是可变的。您可以查看答案吗?
karakale
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.