我有一台运行 nginx 和 varnish 的服务器。我无法同时运行 nginx 和 varnish,因为它们都试图监听端口 80。
nginx 配置为监听端口 8080
upstream www {
server 127.0.0.1:9001;
}
server {
listen 8080 default_server;
server_name server.com;
root /var/www/html/;
location /docs/index.php {
fastcgi_pass www;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
fastcgi_param SERVER_PORT 8888;
}
}
然后 Varnish 监听 80 端口
NFILES=131072
MEMLOCK=82000
NPROCS="unlimited"
RELOAD_VCL=1
VARNISH_VCL_CONF=/etc/varnish/default.vcl
VARNISH_LISTEN_PORT=80
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
VARNISH_SECRET_FILE=/etc/varnish/secret
VARNISH_MIN_THREADS=50
VARNISH_MAX_THREADS=1000
VARNISH_THREAD_TIMEOUT=120
VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
VARNISH_STORAGE_SIZE=1G
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
VARNISH_TTL=120
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
-f ${VARNISH_VCL_CONF} \
-T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
-t ${VARNISH_TTL} \
-w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
-u varnish -g varnish \
-S ${VARNISH_SECRET_FILE} \
-s ${VARNISH_STORAGE}"
当我尝试重新启动 nginx 时,出现错误
service nginx restart
Stopping nginx: [FAILED]
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
如果我停止 varnish 并运行 netstat,我可以看到 nginx 在端口 8080 和 80 上运行
netstat -tulpn | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 95831/nginx
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 95831/nginx
检查 nginx 配置目录,我找不到任何定义端口的地方
grep -rnw '/etc/nginx/' -e '80'
grep -rnw '/etc/nginx/' -e 'server'
/etc/nginx/nginx.conf.rpmnew:40: server {
/etc/nginx/nginx.conf.rpmnew:46: # Load configuration files for the default server block.
/etc/nginx/nginx.conf.rpmnew:52: # redirect server error pages to the static page /40x.html
/etc/nginx/nginx.conf.rpmnew:58: # redirect server error pages to the static page /50x.html
/etc/nginx/nginx.conf.rpmnew:70: # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
/etc/nginx/nginx.conf.rpmnew:88:# Settings for a TLS enabled server.
/etc/nginx/nginx.conf.rpmnew:90:# server {
/etc/nginx/nginx.conf.rpmnew:96:# ssl_certificate "/etc/pki/nginx/server.crt";
/etc/nginx/nginx.conf.rpmnew:97:# ssl_certificate_key "/etc/pki/nginx/private/server.key";
/etc/nginx/nginx.conf.rpmnew:107:# # Load configuration files for the default server block.
/etc/nginx/nginx.conf:40:# server {
/etc/nginx/nginx.conf:46:# # Load configuration files for the default server block.
/etc/nginx/nginx.conf:52: # redirect server error pages to the static page /40x.html
/etc/nginx/nginx.conf:58: # redirect server error pages to the static page /50x.html
/etc/nginx/nginx.conf:70: # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
/etc/nginx/nginx.conf:88:# Settings for a TLS enabled server.
/etc/nginx/nginx.conf:90:# server {
/etc/nginx/nginx.conf:96:# ssl_certificate "/etc/pki/nginx/server.crt";
/etc/nginx/nginx.conf:97:# ssl_certificate_key "/etc/pki/nginx/private/server.key";
/etc/nginx/nginx.conf:107:# # Load configuration files for the default server block.
/etc/nginx/conf.d/virtual.conf:5:#server {
/etc/nginx/conf.d/server.conf:2: server 127.0.0.1:9001;
/etc/nginx/conf.d/server.conf:4:server {
/etc/nginx/conf.d/server.conf:170:server {
/etc/nginx/nginx.conf.default:35: server {
/etc/nginx/nginx.conf.default:50: # redirect server error pages to the static page /50x.html
/etc/nginx/nginx.conf.default:63: # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
/etc/nginx/nginx.conf.default:84: #server {
/etc/nginx/nginx.conf.default:96: # HTTPS server
/etc/nginx/nginx.conf.default:98: #server {
答案1
检查所有服务器块是否具有监听指令,否则 nginx 将默认该块为 80。