I'm trying to set up nginx as a reverse proxy to serve all static assets, whilst forwarding all dynamic requests to Apache.

I've followed a load of different tutorials, none of which are giving me the desired effect.

My /etc/nginx/sites-available/default looks is as follows:

server {
        listen   81 default;
        server_name  localhost;

        access_log  /var/log/nginx/localhost.access.log;

        root /var/www/www.example.com/httpdocs;

        location / {
                try_files $uri @proxy;

        location @proxy {
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size    10m;
                client_body_buffer_size 128k;
                proxy_connect_timeout   90;
                proxy_send_timeout      90;
                proxy_read_timeout      90;
                proxy_buffers           32 4k;
                proxy_pass              http://www.example.com:80;

        location ~ \.php$ {
                proxy_pass              http://www.example.com:80;

        location ~ /\.ht {
                deny  all;

I'm using port 81 so as not to disrupt the running service.

Watching both the nginx and apache access logs I can see that all requests are still hitting both nginx, and being apache, that is all image, css and js requests as well.

If I comment out everything inside @proxy, static assets are served without issue.

As an additional problem I suppose, my rule for location ~ \.php$ is still serving unparsed PHP files.


Have you tried this? Move this line outside of a location block, to the server context:

try_files $uri @proxy;

It sounds like you have confirmed that the static assets you are testing actually exist where you think, as /var/www/www.example.com/httpdocs/$uri;

It could be worth double-checking that this "root" matches what you have set for Apache, and that the user that Nginx is running has permission to access and read those files.

  • Nginx is configured to use the same user www-data as apache, and if I disable the proxy_pass then Nginx serves the static files. I'll try as you suggest though thanks.
    – DanH
    Jul 13 '12 at 2:13

Ok turns out my problem was due to nginx on 81, and apache on 80, and PHP from apache was serving HTML with static asset links with absolute URLs that are not maintaining any port, so these links were defaulting to port 80. Switching nginx to 80 and apache to 81 solved the problem.

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.