在 Nginx+Passenger 上获取 403 Forbidden w. Referer

在 Nginx+Passenger 上获取 403 Forbidden w. Referer

我们使用 Nginx 1.6.2 + Passenger 5.0.6,并使用 PHP-FPM 定义虚拟主机,为 MODx 安装提供服务。只有一个位置 /sinatraapp 为使用 Ruby 2.1.1 的 Sinatra App 提供服务。

设置如下,运行良好,但我们遇到了一个奇怪的问题。当使用 Referer 发出请求时,nginx 会响应 403 Forbidden,而 Sinatra App/Passenger 会返回 302(在这种情况下,302 Redirect 是正确的响应!)。当不使用 Referer 发出请求时,它会按预期工作。

不起作用:

curl -H "Referer: https://www.example.org" https://example.com/sinatraapp

是否有效:

curl -H "Referer: -" https://example.com/sinatraapp

而完全不使用 Referer-Header 也是可行的。Sinatra-App 将 302 将用户引导至 MODx 子站点 example.com/de/something。

Nginx配置:

server {
    listen [::]:443 ssl spdy ipv6only=on;
    listen 443 ssl spdy;
    server_name www.example.com;

    charset utf-8;

    ssl_certificate /etc/ssl/certs/example_com.crt;
    ssl_certificate_key /etc/ssl/private/example_com.key;
    ssl_trusted_certificate /etc/ssl/certs/example_com_bundle.crt;

    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout  10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_dhparam /etc/ssl/certs/dhparam_2048.pem;
    ssl_prefer_server_ciphers on;
    ssl_stapling on;
    #ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=600s;
    resolver_timeout 15s;

    # 1-yr HSTS
    add_header Strict-Transport-Security max-age=31536000;

    root /var/www/www-production;
    index index.php;
    passenger_enabled off;

    # Sinatraapp
    location ~ ^/sinatraapp {
        alias /var/www/sinatraapp/current/public$1;
        passenger_base_uri /sintraapp;
        passenger_app_root /var/www/sinatraapp/current;
        passenger_document_root /var/www/sinatraapp/current/public;
        passenger_enabled on;      
    }
    location ~ ^/(otherdir|justanother) {
        return 301 https://subdomain.example.com$request_uri;
    }
    location ~ ^/(de|en)/favicon.ico {
        rewrite ^/(de|en)/favicon.ico$ /favicon.ico last;
    }
    location ~ ^/(de|en)/assets {
        rewrite ^/(de|en)/assets(.*)$ /assets$2 last;
    }
    location ~ ^/(de|en)/ {
        rewrite ^/(de|en)/(.*)$ /index.php?cultureKey=$1&q=$2 last;
    }
    location / {
            if (!-e $request_filename) {
                    rewrite ^/(.*)$ /index.php?q=$1 last;
            }
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5_example_com_https.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_ignore_client_abort on;
        fastcgi_param  SERVER_NAME $http_host;
    }
    location ~ /\.ht {
            deny  all;
    }

    client_max_body_size 20m;

    access_log /var/log/nginx/example_com_https.access.log;
    error_log /var/log/nginx/example_com_https.error.log;
}

我可以通过设置来解决这个问题

more_set_input_headers 'Referer: -';

在乘客/sinatraapp 位置内,但我不知道为什么会发生这个错误。

相关内容