我正在使用nginx作为代理服务器将请求转发到我的gunicorn服务器上。当我运行sudo nginx -t -c /etc/nginx/sites-enabled/mysite
时,我收到以下错误。
[emerg]: unknown directive "upstream" in /etc/nginx/sites-enabled/mysite:1
configuration file /etc/nginx/sites-enabled/mysite test failed
知道怎么解决吗?这是我的nginx配置:
upstream gunicorn_mysite {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
listen 80;
server_name example.com;
access_log /usr/local/django/logs/nginx/mysite_access.log;
error_log /usr/local/django/logs/nginx/mysite_error.log;
location / {
proxy_pass http://gunicorn_mysite;
}
}
我正在运行Ubuntu 10.04,而我的nginx版本是0.7.65,我是从apt安装的。
这是我运行nginx -V
时的输出nginx version: nginx/0.7.65
TLS SNI support enabled
configure arguments: --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module --with-http_dav_module --with-http_gzip_static_module --with-http_realip_module --with-mail --with-mail_ssl_module --with-ipv6 --add-module=/build/buildd/nginx-0.7.65/modules/nginx-upstream-fair
答案 0 :(得分:20)
当你告诉nginx直接加载该文件时,它会从全局上下文开始。上游指令仅在http上下文中有效。当nginx.conf正常包含该文件时,它已包含在http上下文中:
events { }
http {
include /etc/nginx/sites-enabled/*;
}
您需要使用-c /etc/nginx/nginx.conf或者像上面的块和nginx -c那样制作一个小包装器。
答案 1 :(得分:11)
我在EC2 Ubuntu上使用nginx版本:nginx / 1.4.1。我收到了这个错误:
nginx:在/etc/nginx/nginx.conf中不允许使用[emerg]“upstream”指令
要使我的实例运行,我必须将上游和服务器部分包装在http {}部分中。然后它抱怨失踪事件部分。所以我补充说如下:
活动{ worker_connections 1024; }
在修复之后它运行良好,这是我的第一次努力,所以我在猜测我的方式。
答案 2 :(得分:4)
转动我的nginx配置没问题。问题是我的gunicorn服务器运行不正常。