简体   繁体   English

将Apache重写规则转换为Nginx以提供静态文件

[英]Converting apache rewrite rules to nginx for serving static files

I have a vhost with nginx: 我的Nginx有一个虚拟主机:

  • if it's a static file, send it to apache 如果是静态文件,请将其发送到apache
  • if it's a python file, send it to Python webserver 如果是python文件,则将其发送到Python网络服务器

     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/; } } 

In my Apache, I have those rewrite rules: 在我的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]

But I'm just realizing that it's stupid to pass to Apache: nginx could act itself as a cache for static files! 但是我只是意识到传递给Apache是​​愚蠢的:nginx可以充当静态文件的缓存!

I'd like to rewrite those rules for the nginx server, but so far nothing works. 我想为nginx服务器重写这些规则,但是到目前为止没有任何效果。 I think my big problem is to test if it's a file with the full path and if so, then act as a cache server and send it immediately back. 我认为我的最大问题是测试它是否是具有完整路径的文件,如果是,则充当缓存服务器并立即将其发送回去。

How would you those 8 lines of apache rewrite rules to Nginx ones? 您如何将这8行Apache规则重写为Nginx?

Have you tried with rules like the ones bellow? 您是否尝试过以下规则?

# nginx configuration
location /static {
rewrite ^/static/CACHE/(.*) /static/CACHE/$1;
rewrite ^/static/(.*) /static/production/$1;
}
location /public {
rewrite ^/public/(.*) /uploads/$1;
}
location / {
rewrite ^(.*)$ /$document_root/$1 break;
rewrite ^(.*)$ /$document_root/production/$1 break;
}

You have to try different combinations since nginx rules can be tricky and without actually testing the rules it's really hard to convert them and actually work with the 1st attempt. 您必须尝试不同的组合,因为nginx规则可能很棘手,并且如果不实际测试规则,则很难转换它们并实际进行第一次尝试。

SET THE LOG PATH/FILE 设置日志路径/文件

error_log /var/log/nginx/error.log notice;

INSIDE OF HTTP SCOPE ADD ->

http {
rewrite_log on;

Here's my working configuration 这是我的工作配置

upstream mywebsite {
    ip_hash;
    server 127.0.0.1:8006;
}

server {
    listen *:80;
    server_name mywebsite.com www.mywebsite.com;

    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;

    # ~ = regular expression
    # ~* = regular expression case *insensitive*
    location ~* ^/static/(?<p>.+) {
        root /web/htdocs/mywebsite/htdocs/static;
        try_files /$p /production/$p =403;
        access_log off;
        expires 1h;
    }
    location ~* ^/public/(?<p>.+) {
        root /web/htdocs/mywebsite/htdocs/uploads;
        try_files /$p =403;
        access_log off;
        expires 1h;
    }
    location / {
        expires -1;
        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/;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM