Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use apache as the backend server and nginx on the frontend. Apache listens to port 8080 and nginx to port 80. What I do is have the root point to the public folder foreach virtualhost:

<VirtualHost *:8080>
        ServerAdmin webmaster@localhost
        ServerAlias *
        DocumentRoot /var/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all


And here's the nginx config:

server {
        listen 80;
        access_log /var/log/nginx.access.log;
        error_log /var/log/nginx.error.log;
        root /var/www/;
        index index.php index.html;
        server_name *;
        location / {
    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_cache one;
            proxy_cache_use_stale error timeout invalid_header updating;
            proxy_cache_key $scheme$host$request_uri;
            proxy_cache_valid       200 301 302 20m;
            proxy_cache_valid       404 1m;
            proxy_cache_valid       any 15m;
        location ~ /\.(ht|git) {
                deny all;

The problem is Apache resolves the domain just fine (, but nginx shows instead a 502 Bad Gateway ( I tried looking at the error_log and access_log but I can't find any hint for why can't nginx work.

EDIT: The problem was I wasn't able to include that isolated config for nginx.

share|improve this question
Nothing looks wrong, and you decided not to include the log so it's hard to help... – gparent May 30 '12 at 19:49
bad gateway suggests that you proxy_pass (or fastcgi_pass etc). Where's that piece of config? – 3molo May 30 '12 at 19:50
the log is empty :( – Michelle May 30 '12 at 19:52
@3molo hi thanks, I just included it. – Michelle May 30 '12 at 19:53
Is apache listening to your non localhost ip, or to all ips? (if all ips, are there any firewall rules?) – 3molo May 30 '12 at 19:55

What user is nginx running as? It's possible that the user under which nginx is running cannot access the folder in question.

share|improve this answer
I only chowned it to: chown -R apache:apache /var/www/ How will I know what user nginx is running as? I only run them through ssh root access. – Michelle May 30 '12 at 19:54
Check the nginx config file. It should be in /opt/nginx/conf/nginx.conf The Apache config file has a similar parameter. By default, Apache runs as the "apache" user. – DeeDee May 30 '12 at 19:59
It's obviously related to him proxying the requests, not permissions. – 3molo May 30 '12 at 20:01
I wouldn't be surprised if I'm wrong, but I contend that it's not obviously a proxy issue. – DeeDee May 30 '12 at 20:12
I'm 99% certain that you would only get a bad gateway if a backend is down. prove me wrong and I'll remove my -1 :) – 3molo May 30 '12 at 20:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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