nginx.conf中的expires会在ubuntu上引发错误,但在OSX上不会引发错误

Tom*_*ane 4 ubuntu nginx

我的nginx.conf正在使用本地(OSX),但在prod(Ubuntu)上抛出错误

完整文件:https://github.com/thomasdane/partywave/blob/master/nginx.conf

但相关部分是:

# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/html                  1w;
    text/css                   1w;
    application/javascript     1w;
    ~image/                    1w;
}

server {
    listen       80;
    expires $expires;
    server_name  http://www.partywave.co/;
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的Mac上,这很好用,输出:

curl -I localhost/images/hero.jpg
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Tue, 25 Oct 2016 09:27:51 GMT
Content-Type: image/jpeg
Content-Length: 270624
Connection: keep-alive
X-Powered-By: Express
Accept-Ranges: bytes
Cache-Control: max-age=604800
Last-Modified: Fri, 09 Sep 2016 09:57:09 GMT
ETag: W/"42120-1570e612108"
Expires: Tue, 01 Nov 2016 09:27:51 GMT
Run Code Online (Sandbox Code Playgroud)

但是,当我在生产(Ubuntu 14.04)上运行完全相同的nginx.conf时,我收到以下错误:

nginx: [emerg] "expires" directive invalid value in /etc/nginx/nginx.conf:46
Run Code Online (Sandbox Code Playgroud)

如果我删除$ expires代码,它在生产上工作正常(当然没有过期).

我已经谷歌搜索了一段时间,无法弄清楚为什么.会喜欢任何帮助.

Har*_*ust 9

你在ubuntu中有什么版本的nginx?如果你有14.04的默认仓库,它是1.4.6.并且expires仅接受自1.7.9以来的变量.你可以添加nginx 官方repo并从中安装1.10.半自动安装:

apt-get install -y lsb-release
LIST="/etc/apt/sources.list.d/nginx.list"; OS=`lsb_release -si | tr '[:upper:]' '[:lower:]'`; RELEASE=`lsb_release -sc`; if [ ! -f $LIST ]; then echo -e "deb http://nginx.org/packages/$OS/ $RELEASE nginx\ndeb-src http://nginx.org/packages/$OS/ $RELEASE nginx" > $LIST; else echo "File $LIST exists! Check it."; fi
wget -q -O- http://nginx.org/keys/nginx_signing.key | apt-key add -
apt-get update
apt-get remove -y nginx-full nginx-common
apt-get install nginx
Run Code Online (Sandbox Code Playgroud)