1

Scenario

I want to configure a dedicated nginx reverse proxy that acts as a gateway for multiple backend servers running a traditional lamp stack.

I want to make all requests for any subdomains of example.com and mydomain.com to go to the nginx machine, and send the request to the correct backend. For now i only have one backend server running apache 2.4 with two vhosts, and all requests i make to both subdomains return the content of the same apache vhost.

In a nutshell: site1.example.com and site1.mydomain.com always return the site configured in the apache vhost for site1.example.com

When i tail the access logs of both nginx vhosts i see the requests coming on on both vhosts if i request site1.mydomain.com, with seperate requests bouncing between both vhosts, so it seems something is wrong in my nginx config.

The nginx and apache machine communicate over a vlan with nginx on 192.168.1.2 and the apache machine on 192.168.1.3

Nginx config

backend_01 config file

server {
  server_name site1.example.com;

        location / {
          # app1 reverse proxy
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http://192.168.1.3:80;
        }

   access_log      /var/log/nginx/site1.example.com_access.log;
   error_log       /var/log/nginx/site1.example.com_error.log;

}

server {
  server_name site1.mydomain.com;

        location / {
          # app2 reverse proxy
          proxy_pass http://192.168.1.3:80;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

   access_log      /var/log/nginx/site1.mydomain.com_access.log;
   error_log       /var/log/nginx/site1.mydomain.com_error.log;

}

Apache config

vhost for site1.example.com

<VirtualHost 192.168.1.3:80>

  ServerName site1.example.com
  ServerAlias *.site1.example.com

  DocumentRoot /path/to/docroot
  <Directory /path/to/docroot>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    allow from all
    Require all granted
  </Directory>

    LogLevel error
    ErrorLog /var/log/apache2/site1.example.com_error.log
    CustomLog /var/log/apache2/site1.example.com_access.log combined

</VirtualHost>

vhost for site1.mydomain.com

<VirtualHost 192.168.1.3:80>

  ServerName site1.mydomain.com
  ServerAlias *.site1.mydomain.com

  DocumentRoot /path/to/docroot
  <Directory /path/to/docroot>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    allow from all
    Require all granted
  </Directory>

    LogLevel error
    ErrorLog /var/log/apache2/site1.mydomain.com_error.log
    CustomLog /var/log/apache2/site1.mydomain.com_access.log combined

</VirtualHost>

I have tried setting a default flag in one of the servername directives in the nginx config file but this did not change anything. The nginx.conf file is not manipulated so i don't think something there is causing this but i'll include it anyway just in case.

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

I have tried setting a default server and adding listen 80 to the vhosts but this does not work for me.

I also want to add that i am pretty new to nginx and might be missing something very obvious.

System info:

Ubuntu server 14.04 - 3.12.46-guest-39-a97a54c-x86_64 #4 SMP Mon Aug 10 11:59:25 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

nginx version: nginx/1.4.6 (Ubuntu)

  • You can try replacing <VirtualHost 192.168.1.3:80> with <VirtualHost *:80> in Apache, Does that help ?. I believe there might be issue in binding of default virtual host. – serverliving.com Dec 15 '15 at 10:00
  • Do you see activity in the /var/log/nginx/site1.mydomain.com_access.log? – Richard Smith Dec 15 '15 at 10:02
  • @RichardSmith - I see the requests coming in at both logfiles. Silly i did not check thismyself first ... So there it seems it is going awry at the nginx side – Jake Dec 15 '15 at 10:21
  • @RichardSmith - Solved - i put the wrong name in the config of site1.mydomain.com :-/ – Jake Dec 15 '15 at 11:39
1

Solved.

I had a mismatch in naming between the backend and reverse proxy. The application @ site1.mydomain.com was wrongly configured. For what it's worth, the above config will work if you pay attention to naming on all sides of the setup so it might help some people wanting to set this kind of thing up in the future.

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.