Nginx - reverse proxy a Ghost blog with /subfolder redirect

Florin Gogianu picture Florin Gogianu · Nov 19, 2013 · Viewed 7.7k times · Source

I have a working nginx instance with the rules below. But I'm having difficulties pointing all the requests to domain.com/ghost

I tried modifying the location / {} block to location /ghost/ {} but with no success. I just get a 404 from the ghost app. Any suggestions?

server {
    listen         80;
    server_name domain.com;
    root /home//user/ghost/;
    index index.php;

   # if ($http_host != "domain.com") {
   #      rewrite ^ http://domain.com$request_uri permanent;
   # }

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:2368;
    }

    location ~* \.(?:ico|css|js|gif|jpe?g|png|ttf|woff)$ {
        access_log off;
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public, mustrevalidate, proxy-revalidate";
        proxy_pass http://127.0.0.1:2368;
    }

    location = /robots.txt { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }

    location ~ /\.ht {
            deny all;
    }
}

Answer

djromero picture djromero · Nov 19, 2013

I'm using a regexp location directive for a similar proxy setup. This is the minified configuration file:

worker_processes  1;
pid               /path/to/file.pid;
worker_priority   15;

events {
    worker_connections 512;
    accept_mutex        on;
}

http {
    server {
        error_log   /path/to/log/error.log error;
        listen      127.0.0.1:9000;
        server_name example.com;

        location ~* (/ghost) {
           expires epoch;
           proxy_no_cache 1;
           proxy_pass http://localhost:1234;
        }

        location / {
            proxy_pass http://localhost:1234;
        }
    }
}