使用反向代理按 URL 路径提供不同的资产
Provide different assets by URL path using reverse proxy
我正在尝试提供位于不同 docker 容器中的不同资产。我有两个容器,其中一个是 example.com,另一个是 example.com/site2,我还有另一个容器是 nginx 容器(代理)。
在这个 nginx 容器中,我有一个配置文件可以正确管理站点路径以访问页面,但是无法找到站点 2 中的图像、css 和 javascript(站点 2 中所有资产的 404 状态) .
这是我的配置文件:
server {
listen 80 default_server;
server_name _;
location ~ /\. {
deny all;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 256;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_vary on;
location / {
proxy_pass http://production-site1/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /site2 {
proxy_pass http://production-site2/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
如果我删除以下位置的所有图像,css 和 javascript 会在两个站点中找到:
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
但我想保留这些位置。我的问题有办法解决吗?我尝试了很多在 Whosebug 和其他链接上找到的解决方案,但是 none 这些解决方案有效。
这是我基于 @flaixman answer for nested locations, answer for rewrite URL and this 对否定正则表达式的回答的解决方案。我需要重写 site2 URL,因为 site2 public 文件夹(或 site2 路由)中没有名为 site2 的文件夹。这就是为什么我的资产一直找不到的原因。
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
location ~ ^/site2 {
rewrite ^(/site2)(.*) / break;
proxy_pass http://production-site2;
}
location ~ ^/(?!site2) {
proxy_pass http://production-site1;
}
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
location ~ ^/site2 {
rewrite ^(/site2)(.*) / break;
proxy_pass http://production-site2;
}
location ~ ^/(?!site2) {
proxy_pass http://production-site1;
}
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
我正在尝试提供位于不同 docker 容器中的不同资产。我有两个容器,其中一个是 example.com,另一个是 example.com/site2,我还有另一个容器是 nginx 容器(代理)。
在这个 nginx 容器中,我有一个配置文件可以正确管理站点路径以访问页面,但是无法找到站点 2 中的图像、css 和 javascript(站点 2 中所有资产的 404 状态) .
这是我的配置文件:
server {
listen 80 default_server;
server_name _;
location ~ /\. {
deny all;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 256;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_vary on;
location / {
proxy_pass http://production-site1/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /site2 {
proxy_pass http://production-site2/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
如果我删除以下位置的所有图像,css 和 javascript 会在两个站点中找到:
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
但我想保留这些位置。我的问题有办法解决吗?我尝试了很多在 Whosebug 和其他链接上找到的解决方案,但是 none 这些解决方案有效。
这是我基于 @flaixman answer for nested locations,
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
location ~ ^/site2 {
rewrite ^(/site2)(.*) / break;
proxy_pass http://production-site2;
}
location ~ ^/(?!site2) {
proxy_pass http://production-site1;
}
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
location ~ ^/site2 {
rewrite ^(/site2)(.*) / break;
proxy_pass http://production-site2;
}
location ~ ^/(?!site2) {
proxy_pass http://production-site1;
}
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}