在 nixos 上运行 nginx

在 nixos 上运行 nginx

我正在尝试做什么

我最近在一台计算机上设置了 NixOS,我想将其用作本地网络的服务器。我想使用nginx作为http服务器。

我已经尝试过的

我已将nginx文件放入/etc/nixos/configuration.nixenvironment.systempackages 部分,并在下面放入services.nginx.enable=true.防火墙也被禁用。现在,当我在浏览器中输入服务器的 IP 地址时,没有任何 Web 服务正在运行。当我现在输入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()

即使在之后,这种情况仍然会发生sudo fuser -k 80/tcp。我也尝试添加这个:

  services.nginx = {
    enable = true;
    recommendedProxySettings = true;
    recommendedTlsSettings = true;
    virtualHosts."localhost" = {
      locations."/var/html/".proxyPass = "http://localhost:8000";
    };
    appendHttpConfig = "listen 127.0.0.1:80";  
  };

我的 nixos 配置(包括尝试注释掉一些行)但是当我运行时nixos-rebuild switch输出是:

Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
warning: error(s) occurred while switching to the new configuration

输出systemctl status nginx.service

● nginx.service - Nginx Web Server
     Loaded: loaded (/nix/store/brnpdnprg2x0iapkiilzdl4avg8zfsbh-unit-nginx.service/nginx.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sun 2020-11-15 02:35:09 CET; 3min 59s ago
    Process: 14637 ExecStartPre=/nix/store/vpjfv9bzqrx1klbghh5vn5p9g8w0bs9n-unit-script-nginx-pre-start/bin/nginx-pre-start (code=exited, status=1/FAILURE)
         IP: 0B in, 0B out
        CPU: 47ms

Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Scheduled restart job, restart counter is at 5.
Nov 15 02:35:09 srv0 systemd[1]: Stopped Nginx Web Server.
Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Start request repeated too quickly.
Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Failed with result 'exit-code'.
Nov 15 02:35:09 srv0 systemd[1]: Failed to start Nginx Web Server.

输出journalctl -xe

● nginx.service - Nginx Web Server
     Loaded: loaded (/nix/store/brnpdnprg2x0iapkiilzdl4avg8zfsbh-unit-nginx.service/nginx.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sun 2020-11-15 02:35:09 CET; 6min ago
    Process: 14637 ExecStartPre=/nix/store/vpjfv9bzqrx1klbghh5vn5p9g8w0bs9n-unit-script-nginx-pre-start/bin/nginx-pre-start (code=exited, status=1/FAILURE)
         IP: 0B in, 0B out
        CPU: 47ms

Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Scheduled restart job, restart counter is at 5.
Nov 15 02:35:09 srv0 systemd[1]: Stopped Nginx Web Server.
Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Start request repeated too quickly.
Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Failed with result 'exit-code'.
Nov 15 02:35:09 srv0 systemd[1]: Failed to start Nginx Web Server.

[xoxulux@srv0:~]$ journalctl -xe
░░ The job identifier is 69422.
Nov 15 02:34:59 srv0 nginx-pre-start[14638]: nginx: [emerg] unexpected "}" in /nix/store/ncxk0zwr2y14k8424dwkz4il2qgh0d9h-nginx.conf:49
Nov 15 02:34:59 srv0 nginx-pre-start[14638]: nginx: configuration file /nix/store/ncxk0zwr2y14k8424dwkz4il2qgh0d9h-nginx.conf test failed
Nov 15 02:34:59 srv0 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ An ExecStartPre= process belonging to unit nginx.service has exited.
░░ 
░░ The process' exit code is 'exited' and its exit status is 1.
Nov 15 02:34:59 srv0 systemd[1]: nginx.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ The unit nginx.service has entered the 'failed' state with result 'exit-code'.
Nov 15 02:34:59 srv0 systemd[1]: Failed to start Nginx Web Server.
░░ Subject: A start job for unit nginx.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A start job for unit nginx.service has finished with a failure.
░░ 
░░ The job identifier is 69422 and the job result is failed.
Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Scheduled restart job, restart counter is at 5.
░░ Subject: Automatic restarting of a unit has been scheduled
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ Automatic restarting of the unit nginx.service has been scheduled, as the result for
░░ the configured Restart= setting for the unit.
Nov 15 02:35:09 srv0 systemd[1]: Stopped Nginx Web Server.
░░ Subject: A stop job for unit nginx.service has finished
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A stop job for unit nginx.service has finished.
░░ 
░░ The job identifier is 69482 and the job result is done.
Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Start request repeated too quickly.
Nov 15 02:35:09 srv0 systemd[1]: nginx.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ The unit nginx.service has entered the 'failed' state with result 'exit-code'.
Nov 15 02:35:09 srv0 systemd[1]: Failed to start Nginx Web Server.d
░░ Subject: A start job for unit nginx.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A start job for unit nginx.service has finished with a failure.
░░ 
░░ The job identifier is 69482 and the job result is failed.

我是 NixOS 的新手,所以也许我犯了一个基本错误,那么我应该做什么?

附加信息

内容/nix/store/ncxk0zwr2y14k8424dwkz4il2qgh0d9h-nginx.conf

pid /run/nginx/nginx.pid;
error_log stderr;
daemon off;
events {
}
http {
        # The mime type definitions included with nginx are very incomplete, so
        # we use a list of mime types from the mailcap package, which is also
        # used by most other Linux distributions by default.
        include /nix/store/r2wvgnr54vmwnjvzyqdixv8xbn362jgh-mailcap-2.1.48/etc/nginx/mime.types;
        include /nix/store/d7ccapjb8fpjiryk8lc1sfn8gar9fbhl-nginx-1.18.0/conf/fastcgi.conf;
        include /nix/store/d7ccapjb8fpjiryk8lc1sfn8gar9fbhl-nginx-1.18.0/conf/uwsgi_params;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
        # Keep in sync with https://ssl-config.mozilla.org/#server=nginx&config=intermediate
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:10m;
        # Breaks forward secrecy: https://github.com/mozilla/server-side-tls/issues/135
        ssl_session_tickets off;
        # We don't enable insecure ciphers by default, so this allows
        # clients to pick the most performant, per https://github.com/mozilla/server-side-tls/issues/260
        ssl_prefer_server_ciphers off;
        # OCSP stapling
        ssl_stapling on;
        ssl_stapling_verify on;
        proxy_redirect          off;
        proxy_connect_timeout   90;
        proxy_send_timeout      90;
        proxy_read_timeout      90;
        proxy_http_version      1.0;
        include /nix/store/vzv0ly0l1kv8r1drdg86w9wlipcwdxnw-nginx-recommended-proxy-headers.conf;
        # $connection_upgrade is used for websocket proxying
        map $http_upgrade $connection_upgrade {
                default upgrade;
                ''      close;
        }
        client_max_body_size 10m;
        server_tokens off;
        server {
                listen 0.0.0.0:80 ;
                listen [::]:80 ;
                server_name localhost ;
                location /var/html/ {
                        proxy_pass http://localhost:8000;
                        include /nix/store/vzv0ly0l1kv8r1drdg86w9wlipcwdxnw-nginx-recommended-proxy-headers.conf;
                }
        }
        listen 127.0.0.1:80
}

答案1

Nov 15 02:34:59 srv0 nginx-pre-start[14638]: nginx: [emerg] unexpected "}" in /nix/store/ncxk0zwr2y14k8424dwkz4il2qgh0d9h-nginx.conf:49
Nov 15 02:34:59 srv0 nginx-pre-start[14638]: nginx: configuration file /nix/store/ncxk0zwr2y14k8424dwkz4il2qgh0d9h-nginx.conf test failed
N

问题是,如果你查看第 49 行,你会看到:

listen 127.0.0.1:80

你会发现它缺少一个 ;在线的末尾。这就是问题。如果你查看你的configuration.nix文件,你可以在这里看到:

    appendHttpConfig = "listen 127.0.0.1:80";  

您确实未能;在行尾添加 the 。尝试将其更改为:

    appendHttpConfig = "listen 127.0.0.1:80;";

这应该可以解决你的问题。

相关内容