Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用proxy_传入if语句进行奇怪的重定向_Regex_Apache_Redirect_Nginx - Fatal编程技术网

Regex 使用proxy_传入if语句进行奇怪的重定向

Regex 使用proxy_传入if语句进行奇怪的重定向,regex,apache,redirect,nginx,Regex,Apache,Redirect,Nginx,我有一个SPA(单页应用程序)站点,比如在下,在下有一个API 我想为特定的用户代理提供服务器呈现的内容,比如googlebot,facebook外部点击,等等 所以,如果用户转到它将得到服务SPA,但如果bot转到同一URL,它将得到带有所有正确元和开放图标记的服务服务器呈现页面 “我的服务器呈现的页面与正确匹配”处于启用状态 因此,例如,如果bot命中,它应该从中获取内容 我几乎让它与nginx proxy_pass if语句一起工作到Django应用程序(它返回服务器渲染的输出),但不幸的

我有一个SPA(单页应用程序)站点,比如在下,在下有一个API

我想为特定的用户代理提供服务器呈现的内容,比如
googlebot
facebook外部点击
,等等

所以,如果用户转到它将得到服务SPA,但如果bot转到同一URL,它将得到带有所有正确元和开放图标记的服务服务器呈现页面

“我的服务器呈现的页面与正确匹配”处于启用状态

因此,例如,如果bot命中,它应该从中获取内容

我几乎让它与nginx proxy_pass if语句一起工作到Django应用程序(它返回服务器渲染的输出),但不幸的是,有一种边缘情况使它的行为异常


我的实施:

server {
    listen 80;
    server_name example.com; # url of SPA 
    index index.html;

    root /srv/example_spa/public/dist; # directory of SPA index.html


    # $ssr variable that tells if we should use server side rendered page

    set $ssr 0;
    if ($http_user_agent ~* "googlebot|yahoo|bingbot|baiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|facebot|twitterbot|developers\.google\.com|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator|redditbot") {
        set $ssr 1;
    }


    # location block that serves proxy_pass when the $ssr matches
    # or if the $ssr doesn't match it serves SPA application index.html

    location / {

        if ($ssr = 1) {
            proxy_pass http://127.0.0.1:9505/ssr$uri$is_args$args;
        }

        try_files $uri /index.html;

    }


}

但问题是:

除了一个案例外,所有的东西都很好用

用户点击
https://example.com/brandon/things/
他得到了SPA index.html-完美

用户点击
https://example.com/brandon/things
他得到了SPA index.html-完美

机器人点击
https://example.com/brandon/things/
他得到了服务器呈现的页面-完美

机器人点击
https://example.com/brandon/things
(没有附加斜杠),他被重定向(301)到
https://example.com/ssr/brandon/things
-坏


我已经试着让它工作了好几个小时,但运气不好。 你有什么建议?我知道nginx是不是邪恶的,但我不知道没有它怎么做


非常感谢您的帮助

您需要更改代理通行证的重定向

location / {
    proxy_redirect http://127.0.0.1/ssr/ http://$host/ssr/;
    proxy_redirect /ssr/ /;

    if ($ssr = 1) {
        proxy_pass http://127.0.0.1:9505/ssr$uri$is_args$args;
    }

    try_files $uri /index.html;

}

您需要更改代理通行证的重定向

location / {
    proxy_redirect http://127.0.0.1/ssr/ http://$host/ssr/;
    proxy_redirect /ssr/ /;

    if ($ssr = 1) {
        proxy_pass http://127.0.0.1:9505/ssr$uri$is_args$args;
    }

    try_files $uri /index.html;

}

原来这是Django应用程序重定向的问题。我以为我禁用了“附加斜杠”选项,但它被启用,并在没有斜杠时重定向。并且它重定向到,而不将主机更改为,但仅更改URI部分。因此我感到困惑

实际上我找到了两种方法来解决这个问题


首先,当没有斜杠时,只需使用“重写”来追加斜杠

location / {

    if ($ssr = 1) {
        rewrite ^([^.]*[^/])$ $1/ permanent;
        proxy_pass http://127.0.0.1:9505/ssr$uri$is_args$args;
    }

    try_files $uri /index.html;

}

第二步,修改proxy_pass以始终在$uri part之后添加/斜杠,并在服务器端呈现应用程序url config以接受结尾处的两个斜杠。这是一个有点黑客,但没有副作用和工作,因为它应该

Nginx配置:

location / {

    if ($ssr = 1) {
        proxy_pass http://127.0.0.1:9505/ssr$uri/$is_args$args;
    }

    try_files $uri /index.html;

}
Django URL正则表达式:

r'^ssr/(?P<username>[\w-]+)/(?P<slug>[\w-]+)(/|//)$'
r'^ssr/(?P[\w-]+)/(?P[\w-]+)(/|/)$”

原来这是Django应用程序重定向的问题。我以为我禁用了“附加斜杠”选项,但它被启用,并在没有斜杠时重定向。并且它重定向到,而不将主机更改为,但仅更改URI部分。因此我感到困惑

实际上我找到了两种方法来解决这个问题


首先,当没有斜杠时,只需使用“重写”来追加斜杠

location / {

    if ($ssr = 1) {
        rewrite ^([^.]*[^/])$ $1/ permanent;
        proxy_pass http://127.0.0.1:9505/ssr$uri$is_args$args;
    }

    try_files $uri /index.html;

}

第二步,修改proxy_pass以始终在$uri part之后添加/斜杠,并在服务器端呈现应用程序url config以接受结尾处的两个斜杠。这是一个有点黑客,但没有副作用和工作,因为它应该

Nginx配置:

location / {

    if ($ssr = 1) {
        proxy_pass http://127.0.0.1:9505/ssr$uri/$is_args$args;
    }

    try_files $uri /index.html;

}
Django URL正则表达式:

r'^ssr/(?P<username>[\w-]+)/(?P<slug>[\w-]+)(/|//)$'
r'^ssr/(?P[\w-]+)/(?P[\w-]+)(/|/)$”

你好,谢谢你的回答!事实上,我第二天找到了一个解决方案,我把它添加到了这里。问题不在于nginx,但我的应用程序重定向了。嗨,谢谢你的回答!事实上,我第二天找到了一个解决方案,我把它添加到了这里。问题不在于nginx,而是我的应用程序重定向了。