Kev*_*Kev 8 rewrite nginx haproxy
我们有一个客户的网站在 Apache 上运行。最近,该站点的负载不断增加,作为止损,我们希望将站点上的所有静态内容转移到无 cookie 的域中,例如http://static.thedomain.com.
应用程序不是很好理解。因此,为了让开发人员有时间修改代码以将他们的链接指向静态内容服务器 ( http://static.thedomain.com),我考虑通过 nginx 代理站点并重写传出响应,以便将指向/images/...的链接重写为http://static.thedomain.com/images/....
例如,在从 Apache 到 nginx 的响应中,有一个标题 + HTML 的 blob。在从 Apache 返回的 HTML 中,我们有如下<img>标签:
<img src="/images/someimage.png" />
我想将其转换为:
<img src="http://static.thedomain.com/images/someimage.png" />
这样浏览器在接收到 HTML 页面后直接从静态内容服务器请求图像。
nginx(或HAProxy)可以做到这一点吗?
我粗略地浏览了文档,但除了重写入站 url 之外,没有什么让我感到惊讶。
有一个http://wiki.nginx.org/HttpSubModule - “这个模块可以搜索和替换 nginx 响应中的文本。”
从文档中复制过去:
句法:
sub_filter string replacement
Run Code Online (Sandbox Code Playgroud)
例子:
location / {
sub_filter </head>
'</head><script language="javascript" src="$script"></script>';
sub_filter_once on;
}
Run Code Online (Sandbox Code Playgroud)
小智 3
最好使用代理功能并从适当的位置获取内容,而不是重写 URL 并将重定向发送回浏览器。
代理内容的一个很好的示例如下:
#
# This configuration file handles our main site - it attempts to
# serve content directly when it is static, and otherwise pass to
# an instance of Apache running upon 127.0.0.1:8080.
#
server {
listen :80;
server_name www.debian-administration.org debian-administration.org;
access_log /var/log/nginx/d-a.proxied.log;
#
# Serve directly: /images/ + /css/ + /js/
#
location ^~ /(images|css|js) {
root /home/www/www.debian-administration.org/htdocs/;
access_log /var/log/nginx/d-a.direct.log ;
}
#
# Serve directly: *.js, *.css, *.rdf,, *.xml, *.ico, & etc
#
location ~* \.(js|css|rdf|xml|ico|txt|gif|jpg|png|jpeg)$ {
root /home/www/www.debian-administration.org/htdocs/;
access_log /var/log/nginx/d-a.direct.log ;
}
#
# Proxy all remaining content to Apache
#
location / {
proxy_pass http://127.0.0.1:8080/;
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
Run Code Online (Sandbox Code Playgroud)
在此配置中,nginx 不会将请求重定向到static.domain.com浏览器并期望浏览器发出另一个请求,而是简单地从相关本地路径提供文件。如果请求是动态的,那么代理就会启动并从 Apache 服务器(本地或远程)获取响应,而最终用户对此一无所知。
我希望这有帮助
| 归档时间: |
|
| 查看次数: |
23817 次 |
| 最近记录: |