将apache重写规则转换为nginx以提供静态文件

时间:2017-04-26 13:06:10

标签: python apache nginx url-rewriting

我有一个带有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?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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/;
    }

}