2017-04-26 29 views
0

我有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的?

回答

0

这是我的工作配置

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

} 
0

你有没有尝试像波纹管的规则?

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

你必须尝试不同的组合,因为nginx的规则可能会非常棘手,没有实际测试它真的很难将它们转换,并与第一个尝试的实际工作规则。

设置日志路径/文件

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

INSIDE OF HTTP SCOPE ADD -> 

http { 
rewrite_log on; 
+0

有没有办法做一个重写日志跟踪,比如Apache呢? –

+0

我已经更新了我的答案。你可以这样试试... –