3

我有一个具有以下规则的工作 nginx 实例。但是我很难将所有请求都指向 domain.com/ghost

我尝试将location / {}块修改为location /ghost/ {}但没有成功。我刚从幽灵应用程序中得到一个 404。有什么建议么?

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

3 回答 3

1

已经解决了其他不支持子文件夹的应用程序的类似问题。这两个应用程序都构建在一个平台上,因此它们都尝试在 /fx 目录中工作。我不得不将其中一个放在子文件夹 /gpms 中。

这个想法是将带有引用者的请求从子文件夹重定向到链接到子文件夹之外的目的地 - 我只是将子文件夹添加到此类 uri 的开头。这并不理想,但它有效。

这是我的 nginx 配置:

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

外部链接将被破坏,但这对我来说并不重要,我想它可能会被纠正。

$if_and_hack 用于克服 nginx 对嵌套条件的限制。

顺便说一句,我遇到了 cookie 问题,因为它们设置了路径,并且在重定向后没有为新路径发送 cookie 导致浏览器错误,所以我只是从 cookie 中删除路径。

请注意重写中的完整链接形式 - 这种形式的重写会立即将浏览器重定向到新页面,您不应将其更改为“/gpms/$1”。

作为替代方案,我想,可以使用nginx 模块来检查 html 内容并修改链接。我没有试过这个。或者考虑使用子域而不是子文件夹。

于 2014-01-30T05:46:13.667 回答
1

我正在使用 regexplocation指令进行类似的代理设置。这是缩小的配置文件:

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;
        }
    }
}
于 2013-11-19T18:01:27.747 回答
0

好消息!从 0.4.0 版开始,Ghost 现在支持子目录安装。并且已经有人想通了这一点并创建了教程

于 2014-01-20T05:01:14.470 回答