2017-04-26 99 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

我已經更新了我的答案。你可以這樣試試... –