【发布时间】:2017-09-23 21:53:16
【问题描述】:
我有一个带有 nginx 的虚拟主机:
- 如果是静态文件,发送给apache
-
如果是 python 文件,发送到 Python webserver
server { listen *:80; server_name mywebsite.fr; index index.html index.htm; access_log /var/log/nginx/proxy-access-mywebsite.log proxylog; error_log /var/log/nginx/proxy-error-mywebsite.log error; # vhost for static files location ~ ^/(static|public)/.* { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Server $host; proxy_pass http://apache$uri; } location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Server $host; proxy_pass http://mywebsite/; } }
在我的 Apache 中,我有那些重写规则:
RewriteEngine On
# Remove all /
RewriteRule ^/static/CACHE/(.*) static/CACHE/$1
RewriteRule ^/static/(.*) static/production/$1
RewriteRule ^/public/(.*) uploads/$1
# If file, stop
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [QSA,L]
# Let's try with full path, if file then rewrite + stop:
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule (.*) %{DOCUMENT_ROOT}/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/production/$1 -f
RewriteRule (.*) %{DOCUMENT_ROOT}/production/$1 [QSA,L]
但我只是意识到传递给 Apache 是愚蠢的:nginx 可以充当静态文件的缓存!
我想为 nginx 服务器重写这些规则,但到目前为止没有任何效果。我认为我的大问题是测试它是否是具有完整路径的文件,如果是,则充当缓存服务器并立即将其发送回来。
你会如何将这 8 行 apache 规则重写为 Nginx 规则?
【问题讨论】:
标签: python apache nginx url-rewriting