4

I am new in nginx. I want to rewrite

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

to

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

with request parameters.

I use official nginx docker image. How should be my nginx.conf file?

EDIT: 'feature-1234' is a variable so:

feature-5678.mydomain.com

should serve as

docker-feature-5678:9000

1

By combining answers, i found the solution.

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;

    }
  }
}

Thanks

0

site.conf

server_tokens off;

map $http_upgrade $connection_upgrade { default upgrade; ''
close; }

server {

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

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

access_log /$access_log; error_log /$error_log;

location / {

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;

} }

paste it on

touch /etc/nginx/sites-available/site.conf

next

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

next

service nginx reload

if you don't root access add sudo to beginning of the commands

  • thanks for the answer but i updated the question to be more clear. Feature name will be variable. Can you review your answer? – karakale Jul 26 '17 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.