Proxy a subdomain with nginx
Thursday, 09 August 2012
Écrit par
Grégory Soutadé

A lot of things has been written about nginx and Apache : proxy_pass, proxy_redirect, subdomains... I just want to publish my configuration that is in test but works. This is a response to my requirements : I want nginx to serve the subdomain but transfer all other requests to an Apache server (,, ...).


First step is to install nginx. Nginx current version is 0.7.23 (thanks to debian stable). Then edit /etc/nginx/sites-available/default :

server { listen 80 default; ## listen for ipv4 server_name *; access_log /var/log/nginx/; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; resolver localhost; proxy_pass http://$host:8000; } } server { listen 80; ## listen for ipv4 server_name; access_log /var/log/nginx/; location / { root /var/www/blog; index index.html; } location = /favicon.ico { access_log off; log_not_found off; } location ~ /\. { deny all; access_log off; log_not_found off; } }

The first block tells nginx to redirect all request from and * to a local Apache server listening on port 8000. The second block creates a special rule for : files will be served by nginx server and no redirection will be applied. We also disable favicon.ico error log and deny serving \.* files. There is a tip in the first block : we need to set up a local DNS server ("resolver localhost" directive). If this option is not set, it will try to do another DNS request to resolve $host address. So we'll simply install bind9 and configure it. Edit /etc/bind/named.conf.local

zone "" { type master; file "/etc/bind/"; };

Finally edit /etc/bind/ with :

$TTL 604800 @ IN SOA ( 07082012 ; Serial 604800 ; Refresh 86400 ; Retry 2419200 ; Expire 604800 ) ; Negative Cache TTL ; @ IN NS localhost. @ IN A * IN CNAME @ IN AAAA ::1

And restart bind : sudo service bind9 restart. is the mail address of the administrator. Now everything might be ok.

Auteur :

e-mail* :

Le commentaire :

* Seulement pour être notifié d'une réponse à cet article