Nginx - reverse proxy a Ghost blog with /subfolder redirect

前端 未结 3 747
悲&欢浪女
悲&欢浪女 2021-01-14 15:33

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

相关标签:
3条回答
  • 2021-01-14 16:16

    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.

    0 讨论(0)
  • 2021-01-14 16:26

    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.

    0 讨论(0)
  • 2021-01-14 16:28

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题