Nginx“無法啟動高效能 Web 伺服器和反向代理伺服器。”

Nginx“無法啟動高效能 Web 伺服器和反向代理伺服器。”

我的 ssl 憑證最近過期了。為了更新證書,我使用了 certbot,但現在證書將由我的託管自動更新。但在此之前,我應該在伺服器上進行從 http 到 https 的重定向。你能幫我嗎?

systemctl 狀態 nginx.service:

nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en
   Active: failed (Result: exit-code) since Sun 2019-08-18 07:42:13 CEST; 2 days
     Docs: man:nginx(8)
  Process: 11855 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5
  Process: 11590 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s
  Process: 27222 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (cod
  Process: 27214 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process
 Main PID: 11583 (code=exited, status=0/SUCCESS)

Aug 18 07:42:11 vps685363 nginx[27222]: nginx: [emerg] bind() to 0.0.0.0:443 fai
Aug 18 07:42:11 vps685363 nginx[27222]: nginx: [emerg] bind() to 0.0.0.0:80 fail
Aug 18 07:42:12 vps685363 nginx[27222]: nginx: [emerg] bind() to 0.0.0.0:443 fai
Aug 18 07:42:12 vps685363 nginx[27222]: nginx: [emerg] bind() to 0.0.0.0:80 fail
Aug 18 07:42:12 vps685363 nginx[27222]: nginx: [emerg] bind() to 0.0.0.0:443 fai
Aug 18 07:42:12 vps685363 nginx[27222]: nginx: [emerg] bind() to 0.0.0.0:80 fail
Aug 18 07:42:13 vps685363 nginx[27222]: nginx: [emerg] still could not bind()
Aug 18 07:42:13 vps685363 systemd[1]: nginx.service: Control process exited, cod
Aug 18 07:42:13 vps685363 systemd[1]: nginx.service: Failed with result 'exit-co
Aug 18 07:42:13 vps685363 systemd[1]: Failed to start A high performance web ser

nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

     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;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

預設:

server {
    server_name domukasi.pl www.domukasi.pl;
        location / {


                proxy_pass http://127.0.0.1:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }



    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domukasi.pl/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domukasi.pl/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}server {
    if ($host = www.domukasi.pl) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = domukasi.pl) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name domukasi.pl www.domukasi.pl;
    listen 80;
    return 404; # managed by Certbot




}

我的網站(domukasi.pl)上出現此錯誤:

ERR_TOO_MANY_REDIRECTS

我也無法重新啟動我的 nginx 伺服器。

答案1

奇怪的是,您ERR_TOO_MANY_REDIRECTS的瀏覽器中出現錯誤,因為您的nginx伺服器沒有啟動。

似乎其他一些伺服器已經在偵聽連接埠 80 和 443,這導致這些重定向並導致nginx無法啟動。

在錯誤日誌中你得到

Aug 18 07:42:12 vps685363 nginx[27222]: nginx: [emerg] bind() to 0.0.0.0:443 fai
Aug 18 07:42:12 vps685363 nginx[27222]: nginx: [emerg] bind() to 0.0.0.0:80 fail

這表明nginx無法綁定到這些端口,這表明它們要么已被另一個進程使用,要么nginx沒有足夠的權限來綁定到這些端口(因為它們低於 1024)。我認為您已經在這台機器上運行了另一台正在使用這些連接埠的伺服器。

您可以透過發出命令來檢查這些連接埠是否已被使用

sudo lsof -i :80

sudo lsof -i :443

或者

sudo netstat -ltnp | grep -w ':80'

sudo netstat -ltnp | grep -w ':443'


嘗試這個預設文件

##############################################
#
#   HTTP (port 80) domukasi.pl
#
##############################################

server {

    server_name domukasi.pl www.domukasi.pl;
    listen 80;

    # redirect EVERYTHING from HTTP to HTTPS
    location / {
      return 301 https://$host$request_uri;
    }
}

##############################################
#
#   HTTPS (port 443) domukasi.pl
#
##############################################

server {

    server_name domukasi.pl www.domukasi.pl;
    listen 443 ssl; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/domukasi.pl/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domukasi.pl/privkey.pem; # managed by Certbot

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    #######################################
    # location /websocket {
    #   # proxy_http_version 1.1;
    #   proxy_read_timeout 31536000;
    #   proxy_set_header Upgrade $http_upgrade;
    #   proxy_set_header Connection "upgrade";
    #   proxy_pass http://127.0.0.1:3000/websocket;
    # }
    #######################################

    location / {
      proxy_http_version 1.1;
      proxy_set_header  Host $host;
      proxy_set_header  Name $server_name;
      proxy_set_header  X-Forwarded-Host $host;
      proxy_set_header  X-Forwarded-Name $server_name;
      proxy_set_header  X-Forwarded-IP   $remote_addr;
      proxy_set_header  X-Forwarded-Port $server_port;
      proxy_set_header  X-Forwarded-Remote-IP   $remote_addr;
      proxy_set_header  X-Forwarded-Remote-Port $remote_port;
      proxy_set_header  X-Forwarded-Server-IP   $server_addr;
      proxy_set_header  X-Forwarded-Server-Port $server_port;
      proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
      proxy_set_header  X-Forwarded-Proto $scheme;
      proxy_pass http://127.0.0.1:3000/;
    }
}

相關內容