稍后编辑经过大量的故障排除之后,实际的问题在server_name指令之后是一个缺失的分号。 nginx -t -c /etc/nginx/nginx.conf没有抓住它。 如果遇到与此相似的情况,请仔细检查拼写错误。
原来的问题如下:
我正在使用nginx 1.10.0build立一个基于ubuntu 16.04的新服务器。
具体问题是,虽然我的新configuration基本上与使用nginx 1.4.4的ubuntu 13.10服务器上的旧nginxconfiguration相匹配,但nginx 1.10.0仅创buildipv4或ipv6 worker,但不能同时configuration。 旧的服务器上不存在此行为。 不知道还有什么要尝试在这一点上。
我已经validation了我的nginx安装是使用ipv6构build的。
nginx version: nginx/1.10.0 (Ubuntu) built with OpenSSL 1.0.2g-fips 1 Mar 2016 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads
以下是我目前的新服务器configuration:
# /etc/nginx/nginx.conf user www-data; worker_rlimit_nofile 30000; worker_processes 8; pid /run/nginx.pid; events { worker_connections 500000; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
我现在已经启用了一个站点进行testing。 我将最终configuration多个虚拟主机。
# /etc/nginx/sites-enabled/blog server { server_name test.bloggyblog.com listen 80; listen [::]:80; root /usr/local/apps/blog; index index.php; location / { try_files $uri $uri/ =404; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
最后,奇怪的是工作人员绑定到ipv4还是ipv6完全取决于listen命令的放置顺序。 在以下数据中,我已经切换了顺序并尝试了多次不同的configuration。 每次更改/etc/nginx/sites-enabled/blog我做了sudo service nginx stop; sudo service nginx start; sudo lsof -i; sudo service nginx stop; sudo service nginx start; sudo lsof -i; 获取数据。
另外请注意,在执行这些步骤后,我将工人数量更改为8。 然而,当工人数量增加时,所有工人都是ipv4或ipv6的行为也是一样的。
listen [::]:80; listen 80; nginx 27675 root 6u IPv4 204423 0t0 TCP *:http (LISTEN) nginx 27676 www-data 6u IPv4 204423 0t0 TCP *:http (LISTEN) listen 80; listen [::]:80; nginx 27747 root 6u IPv6 205134 0t0 TCP *:http (LISTEN) nginx 27748 www-data 6u IPv6 205134 0t0 TCP *:http (LISTEN) listen 80; listen [::]:80 default ipv6only=on; nginx 27819 root 6u IPv6 205849 0t0 TCP *:http (LISTEN) nginx 27820 www-data 6u IPv6 205849 0t0 TCP *:http (LISTEN) listen 80; listen [::]:80 default ipv6only=off; nginx 27885 root 6u IPv6 206495 0t0 TCP *:http (LISTEN) nginx 27886 www-data 6u IPv6 206495 0t0 TCP *:http (LISTEN) listen 80; listen [::]:80 default; nginx 27953 root 6u IPv6 207184 0t0 TCP *:http (LISTEN) nginx 27954 www-data 6u IPv6 207184 0t0 TCP *:http (LISTEN)
它看起来像你的ipv6only的默认设置是不同的。 在大多数操作系统上,您可以创build也可以接受IPv4连接的IPv6套接字,因此只需要一个套接字(一个listen指令)。
看来你的旧服务器上它使用ipv6only=on所以你创build了一个IPv4和IPv6套接字。 在您的新服务器上,默认值是ipv6only=off ,这也使得IPv6套接字也在IPv4上侦听。 这会与单独的IPv4套接字产生冲突。 如果你删除了IPv4的listen线,那么它可能只适用于两种协议。
为了使事情可预测,最好明确地设置ipv6only标志,并使用其中的一个:
listen 80; listen [::]:80 ipv6only=on;
要么
listen [::]:80 ipv6only=off;
所以我终于能够解决这个问题,但不是以其他任何方式描述过的。
我在/etc/sysctl.conf中设置了“net.ipv6.bindv6only = 1”,并通过“sudo sysctl -p”重新加载。 然后在服务器指令“listen 80; listen [::]:80;”中进行configuration 我现在让我的工作人员平均分配ipv4和ipv6,现在我可以连接从任何IP源。
添加到/etc/sysctl.conf并使用“sudo sysctl -p”重新加载:
net.ipv6.bindv6only = 1
示例nginxconfiguration:
server { server_name test.bloggyblog.com listen 80; listen [::]:80; root /usr/local/apps/blog; index index.php; location / { try_files $uri $uri/ =404; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
重新加载nginx
sudo service nginx reload
然后“sudo lsof -i”
nginx 2096 www-data 6u IPv4 286321 0t0 TCP *:http (LISTEN) nginx 2096 www-data 7u IPv6 286322 0t0 TCP *:http (LISTEN) nginx 2097 www-data 6u IPv4 286321 0t0 TCP *:http (LISTEN) nginx 2097 www-data 7u IPv6 286322 0t0 TCP *:http (LISTEN) nginx 2098 www-data 6u IPv4 286321 0t0 TCP *:http (LISTEN) nginx 2098 www-data 7u IPv6 286322 0t0 TCP *:http (LISTEN) nginx 2099 www-data 6u IPv4 286321 0t0 TCP *:http (LISTEN) nginx 2099 www-data 7u IPv6 286322 0t0 TCP *:http (LISTEN) nginx 2100 www-data 6u IPv4 286321 0t0 TCP *:http (LISTEN) nginx 2100 www-data 7u IPv6 286322 0t0 TCP *:http (LISTEN) nginx 2101 www-data 6u IPv4 286321 0t0 TCP *:http (LISTEN) nginx 2101 www-data 7u IPv6 286322 0t0 TCP *:http (LISTEN) nginx 2102 www-data 6u IPv4 286321 0t0 TCP *:http (LISTEN) nginx 2102 www-data 7u IPv6 286322 0t0 TCP *:http (LISTEN) nginx 2103 www-data 6u IPv4 286321 0t0 TCP *:http (LISTEN) nginx 2103 www-data 7u IPv6 286322 0t0 TCP *:http (LISTEN)
用“netstat -tlp”确认
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 *:http *:* LISTEN - tcp6 0 0 [::]:http [::]:* LISTEN -