【问题标题】:Converting apache rewrite rules to nginx for serving static files将 apache 重写规则转换为 nginx 以提供静态文件
【发布时间】:2017-09-23 21:53:16
【问题描述】:

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

【问题讨论】:

    标签: python apache nginx url-rewriting


    【解决方案1】:

    您是否尝试过以下规则?

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

    【讨论】:

    • 有没有办法像 apache 一样重写日志跟踪?
    【解决方案2】:

    这是我的工作配置

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

    【讨论】:

      猜你喜欢
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2016-08-09
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多