0

I currently have an nginx VM setup to route traffic between 3 domains but am having an issue with routing the subdomains at the moment. The current configuration goes PFSense -> nginx reverse proxy -> apache2. I am able to access the root domain fine but any time I enter a subdomain I am routed to the root instead.

Here is my server block configuration, there are some additional items in there because I need to be able to handle websockets as well.

    server
{
        listen 80 default_server;
        server_name .mydomain.com;

        location /
        {
                proxy_pass http://<internal_server>;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
        }
}

server
{
        listen 443 default_server;

        server_name .mydomain.com;

        ssl_certificate /etc/nginx/secure/nproxy.crt;
        ssl_certificate_key     /etc/nginx/secure/nproxy.key;

        ssl on;
        ssl_session_cache       builtin:1000    shared:SSL:10m;
        ssl_protocols   TLSv1.2;
        ssl_ciphers     HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers on;

        access_log      /var/log/nginx/secureproxy.log;

        location /
        {
                proxy_pass http://<internal_server>;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
        }
}

2 Answers 2

0

While nginx supports wildcards and regular expression in server_name, you still have to specify the subdomain while proxying. This can be done using the $host variable:

server {
    server_name .mydomain.com;
    listen 80;
    location / {
        proxy_pass http://$host;
    }
    access_log /var/log/nginx/$host-access.log;
    error_log /var/log/nginx/all-hosts-error.log;
}

This will proxy a request for foo.mydomain.com to in an internal foo.mydomain.com. If the internal and external domains don't match, use a grouped regular expression:

server {
    server_name ~^(?<subdomain>.+)\.mydomain\.com$;
    listen 80;
    location / {
        proxy_pass http://$subdomain.internal.host;
    }
    access_log /var/log/nginx/$host-access.log;
    error_log /var/log/nginx/all-hosts-error.log;
}
6
  • I've been doing the proxy_pass by IP, is domain required to make this work?
    – user186658
    Feb 3, 2016 at 8:22
  • @user186658 How does your internal server process subdomains?
    – muru
    Feb 3, 2016 at 8:24
  • Apache virtualhosts. The proxy configuration is new, I previously just did a NAT passthrough directly to my apache server.
    – user186658
    Feb 3, 2016 at 8:29
  • Then you'll probably have to use domains.
    – muru
    Feb 3, 2016 at 8:46
  • Now I feel stupid....the subdomain is set to only listen for https but the proxy_pass directive is passing to http. Thank you for the information though, I will probably end up converting to domain translation since it will likely be more flexible in the future.
    – user186658
    Feb 3, 2016 at 8:58
0

Between making modifications based on this post: https://serverfault.com/questions/598202/make-nginx-to-pass-hostname-of-the-upstream-when-reverseproxying

and re-reading my configuration I managed to get it working.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .