为什么nginx需要重新启动?

为什么nginx需要重新启动?

Ubuntu 16.04 在启动时启动 nginx。它配置为代理本地主机上的几个开发网站。当 nginx 首次启动时,这些网站尚未运行。

如果我启动网站并浏览https://dev.mysite.com(在 etc/hosts 中配置),浏览器会显示连接被拒绝。然后我重新启动 nginx,它们就可以连接了。为什么第一次被拒绝?

☀  ps aux | grep nginx
root      1805  0.0  0.0 129336  2348 ?        Ss   Sep22   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
... other processes

☔  sudo /usr/sbin/nginx -T
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;

... details

server {
    listen 80;
    return 307 https://$http_host$request_uri;
}


# Map hosts -> meteor apps.

map $http_host $backend {
  site1-dev.site.io    http://127.0.0.1:3006;
  site2-dev.site.io    http://127.0.0.1:3000;
  site3-dev.site.io    http://127.0.0.1:3010;
  site4-dev.site.io    http://127.0.0.1:3012;
  site5-dev.site.io    http://127.0.0.1:3014;
}

# Proxy web-socket connections
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

# Serve HTTPS content
server {
    listen 443 ssl http2;
    server_name *.site.io; # this domain must match Common Name (CN) in the SSL certificate

    ssl_certificate /etc/ssl/site.crt;
    ssl_certificate_key /etc/ssl/site.key;

    # performance enhancement for SSL
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;

    # safety enhancement to SSL: make sure we actually use a safe cipher
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   # don’t use SSLv3 ref: POODLE

    # Forward secrecy
    ssl_dhparam /etc/ssl/dhparam.pem;

    add_header Strict-Transport-Security "max-age=31536000;";

    # pass all requests to Node
    location / {
        proxy_pass $backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; # allow websockets
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP
        add_header Cache-Control no-cache;  
    }
}

这很简单,但是除非我重新启动 nginx,否则我的浏览器将无法连接它们:

☀  sudo service nginx start
☔  sudo /usr/sbin/nginx -T
... Same output, but now browser can connect

这有什么大不了的?不。这只是一个额外的步骤,我不明白为什么。我可以关闭/重新启动网站,第一次之后再也不需要重新启动 nginx。

为什么nginx启动后需要重新启动?

更新:

因此,计算机启动、登录,然后开始工作: ☔ sudo netstat -tulpen|grep nginx (no output) ☔ ps aux | grep nginx root 1843 0.0 0.0 129336 2320 ? Ss 08:53 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; (workers follow) ☔ sudo /usr/sbin/nginx -T
(no output)

然后重新启动sudo service nginx restart ☀ sudo netstat -tulpen|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 167492 15135/nginx -g daem tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 0 167493 15135/nginx -g daem

我在看,但在日志中看不到“绑定”错误。

相关内容