简体   繁体   English

nginx重写/ proxy_pass问题

[英]nginx rewrite / proxy_pass problems

I did some extensive searching and there are quite a few posts on nginx proxy_pass questions. 我进行了广泛的搜索,关于nginx proxy_pass问题的帖子很多。 I've tried modifying my problem to some of those questions with out any progress so here goes. 我尝试将我的问题修改为其中一些问题,但没有任何进展,因此请继续。

I'm in the midst of rewriting a php based website into Rails. 我正在将基于PHP的网站重写为Rails。 The original site itself is simple 2 pages with forms. 原始站点本身是包含表单的简单2页。 It uses a mod_rewrite / mod_proxy solution in Apache to mask the url of where the site continues after form post. 它在Apache中使用mod_rewrite / mod_proxy解决方案来掩盖表单发布后站点继续所在位置的url。

the php site has 3 directories that have nothing but 3 .htaccess files in them for simplicity sake lets call the directories a, b, c. php站点具有3个目录,其中仅包含3个.htaccess文件,为简单起见,我们将目录称为a,b,c。 each .htaccess file with the following 每个具有以下内容的.htaccess文件

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.domainbeingpostedto.com/actual_letter_directory/$1 [P,L]
</IfModule>

I'm not an apache expert but I'm pretty sure the [P,L] is same as proxy_pass and last in nginx? 我不是apache专家,但是我很确定[P,L]与proxy_pass相同,并且最后出现在nginx中吗?

I'm trying to rewrite this solution for the php site that was converted into a rails cms using passenger and nginx instead. 我正在尝试为该PHP站点重写此解决方案,该站点已使用passenger和nginx转换为rails cms。

The solution I have so far is not working because the rails app just returns a 404 with the page not found, so I know proxy_pass isn't forwarding the post request to the other server. 到目前为止,我的解决方案无法正常工作,因为Rails应用程序仅返回未找到页面的404,因此我知道proxy_pass不会将发布请求转发至其他服务器。 What I have for my nginx.conf file is: 我的nginx.conf文件具有以下功能:

server {
    listen 80;
    server_name newtestappdomain.com;

    location /a/ {
        proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
        #rewrite ^/a/(.*)$ http://www.domaintoacceptdatapostandbemaskedcom/a/ last;
    }

    location /b/ {
        proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
        #rewrite ^/b/(.*)$ http://www.domaintoacceptdatapostandbemasked.com/b/ last;
    }

    location /c/ {
        proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
        #rewrite ^/c/(.*)$ http://www.domaintoacceptdatapostandbemasked.com/c/ last;

    }

    root /home/deploy/testapp/public;   # <--- be sure to point to 'public'!
    passenger_enabled on;

}

If I uncomment out the rewrite rule, it just bounces to the other site which i'm trying to mask. 如果我取消注释重写规则,它只会反弹到我要屏蔽的其他站点。 I did a header trace to verify as well. 我也做了标题跟踪以进行验证。 Didn't see anything post to the other domain. 没有看到发布到其他域的任何内容。 I'm kind of stumped as I'm really new to nginx and not sure what to do. 我有点不高兴,因为我真的是nginx的新手,不确定该怎么做。 Apache doesn't work well with rails and mod_rewrite / mod_proxy. Apache在rails和mod_rewrite / mod_proxy上不能很好地工作。 Any insight would be great. 任何见识都会很棒。

proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;

This rule would be proxy requests to "/" location (leading slash in proxy_pass uri) (a/, b/ and c/ would be lost). 此规则将是对“ /”位置的代理请求(proxy_pass uri中的斜杠开头)(a /,b /和c /将丢失)。

Just use uri without leading slash and it should work perfect 只需使用uri而不使用斜杠,它应该可以完美工作

proxy_pass http://www.domaintoacceptdatapostandbemasked.com;

If you need to change uri, you can use rewrite before proxy_pass. 如果需要更改uri,可以在proxy_pass之前使用rewrite。 for example: 例如:

location /a/ {
    rewrite /(a/.*)$ /actual_letter_directory/$1 break; # as from you .htaccess example
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM