Nginx 相对 URL 到绝对重写规则? [英] Nginx Relative URL to Absolute Rewrite Rule?

查看:65
本文介绍了Nginx 相对 URL 到绝对重写规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下配置:

server {

    listen 80;
    server_name site.com;

    location /proxy {
            proxy_pass   http://newsite.com/;
   }
 }

site.com 上托管的页面对其所有静态文件使用相对路径 (/main.css),因此当我收到代理页面时,浏览器正在查询具有错误路径的静态文件:

The page hosted on site.com uses relative path (/main.css) for all it statics files, so when i receive the proxied page, the browser is querying static files with the wrong path:

即: http://site.com/main.css

我将如何让这些请求的静态文件跟随到它们的原始位置 (http://newsite.com/main.css)?

How would i go to have these static files requested follow to their original location (http://newsite.com/main.css)?

推荐答案

您应该能够使用 重写server 部分中的规则,类似于以下内容(未经测试):

You should be able to use a rewrite rule in your server section, something like the following (untested):

server_name site.com;
rewrite ^/([^/]+\.css)$ http://newsite.com/$1;

这应该会将所有对 site.com 顶级目录中的 .css 文件的请求重定向到 http://newsite.com/.如果您需要包含其他静态文件(图像、脚本等),请根据需要调整正则表达式或添加重写规则.

That should redirect all requests for .css files in the top-level directory of site.com to http://newsite.com/. Adjust the regex or add rewrite rules as necessary if you need to include other static files (images, scripts, etc.).

这篇关于Nginx 相对 URL 到绝对重写规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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