Nginx - reverse proxy a Ghost blog with /subfolder

2020-03-26 12:08发布

问题:

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;
    }
}

回答1:

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;
        }
    }
}


回答2:

Have solved similar problem with other apps which have no support for subfolders. Both apps are built on one platform, so they both tries to work in /fx dir. I had to place one of them in to subfolder /gpms .

The idea is to redirect requests with referer from subfolder to destinations which links outside of subfolder - i just add subfolder to beginning of such uris. It is not ideal, but it works.

Here is my nginx config:

    server {
    listen 80;
    server_name mydomain.com;

    location / {
    rewrite ^/$ /fx/;
    proxy_pass http://127.0.0.1:56943/;
    proxy_redirect     off;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_read_timeout 300;
    }

     error_log /var/log/nginx/debug.log debug;
     set $if_and_hack "";
     if ( $http_referer ~ '^http://mydomain.com/gpms/.*$' ) {
        set $if_and_hack "refgpms";
        }
     if ( $uri !~ '^/gpms/.*$' ) {
        set $if_and_hack "${if_and_hack}_urinogpms";
     }
     if ( $if_and_hack = "refgpms_urinogpms" ) {
        rewrite  ^/(.*)$ http://$host/gpms/$1;
        }

    location /gpms/ {
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_cookie_path  /fx /;
    proxy_pass http://127.0.0.1:12788/fx/;
    proxy_redirect     default;
    }
}

External links will be broken, but it is not critical for me and i guess it may be corrected.

$if_and_hack is for overcome nginx limitation on nested conditions.

By the way i have got a cookies issue, because they was set with path, and hit browser bug with not sending cookies for a new path after redirect, so i just remove path from cookies.

Note full link form in rewrite - this form of rewrite immediately redirects browser to new page, you should not change it to just "/gpms/$1".

As alternative, i guess, it may be possible to use nginx module to inspect html content and modify links. I have not tried this. Or consider to use subdomains instead of subfolders.



回答3:

Good news! As of version 0.4.0 Ghost now supports subdirectory installation. And there are already people who've figured this out and created tutorials.