我有nginx的一个虚拟主机:转换阿帕奇重写规则nginx的提供静态文件
- 如果它是一个静态文件,发送到apache的
如果它是一个Python文件,将它发送到Python Web服务器
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的?
有没有办法做一个重写日志跟踪,比如Apache呢? –
我已经更新了我的答案。你可以这样试试... –