Nginx-使用/subfolder重定向反向代理Ghost博客 [英] Nginx - reverse proxy a Ghost blog with /subfolder redirect

查看:236
本文介绍了Nginx-使用/subfolder重定向反向代理Ghost博客的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试将location / {}块修改为location /ghost/ {},但没有成功.我刚刚从幽灵应用程序中获取了404.有什么建议吗?

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

推荐答案

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

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 .

这个想法是将带有引荐来源网址的请求从子文件夹重定向到链接到子文件夹外部的目标-我只是将子文件夹添加到此类uri的开头.这不是理想的方法,但是可行.

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.

这是我的Nginx配置:

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用于克服nginx对嵌套条件的限制.

$if_and_hack is for overcome nginx limitation on nested conditions.

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

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.

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

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".

我猜作为替代,也许可以使用 nginx模块检查html内容并进行修改链接.我还没有尝试过.或者考虑使用子域代替子文件夹.

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.

这篇关于Nginx-使用/subfolder重定向反向代理Ghost博客的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆