【问题标题】:How to run Nginx on a different port other than 80如何在 80 以外的其他端口上运行 Nginx
【发布时间】:2019-09-15 17:43:36
【问题描述】:

我无法运行 Nginx,因为端口 80 已经在监听 docker-proxy 服务。

tcp6     0     0 :::80          :::*           LISTEN      13110/docker-proxy

我想在端口 8800 而不是默认端口 80 上运行 Nginx。

因此,我已将默认文件编辑如下;

sudo nano /etc/nginx/sites-available/default

listen 8800 default_server;
listen [::]:8800 default_server;
listen localhost;

但是,即使重新启动,我仍然无法按预期工作。

我做错了什么,我该如何解决?

以下是我得到的错误;

● nginx.service - 高性能 Web 服务器和反向代理服务器 已加载:已加载(/lib/systemd/system/nginx.service;已启用;供应商预设:已启用)
活动:自美国东部夏令时间周五 2019 年 4 月 26 日 04:23:14 起失败(结果:退出代码); 13 分钟前
文档:man:nginx(8)
进程:16955 ExecStart=/usr/sbin/nginx -g 守护进程开启; master_process 开启; (code=exited, status=1/FAILURE)

进程:16944 ExecStartPre=/usr/sbin/nginx -t -q -g 守护进程开启; master_process 开启; (代码=退出,状态=0/成功)
主 PID:80941(代码=退出,状态=0/成功)
4 月 26 日 04:23:11 ubuntu systemd[1]:启动高性能 Web 服务器和反向代理服务器...
Apr 26 04:23:11 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use) Apr 26 04:23:12 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
4 月 26 日 04:23:12 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
4 月 26 日 04:23:14 ubuntu nginx[16955]:nginx:[emerg] 仍然无法绑定()
Apr 26 04:23:14 ubuntu systemd[1]: nginx.service: 控制进程退出,code=exited status=1
4 月 26 日 04:23:14 ubuntu systemd[1]:nginx.service:失败,结果为“退出代码”。
4 月 26 日 04:23:14 ubuntu systemd[1]: 无法启动高性能 Web 服务器和反向代理服务器。

【问题讨论】:

标签: nginx


【解决方案1】:

你必须去 /etc/nginx/sites-enabled/default

编辑该文件并放入(如果您愿意将“8800”作为您的 nginx 端口)

server { listen 8800; }

启动服务器

sudo 服务 nginx 启动

sudo service nginx restart 如果之前运行过。

然后访问localhost:8800

向 iptables 添加规则

 vi /etc/sysconfig/iptables 
 -A INPUT -m state --state NEW -m tcp -p tcp --dport 8800 -j ACCEPT

重启 IPtables

sudo service iptables restart;

【讨论】:

  • 我已经尝试过启用站点和可用站点。都没有奏效。请查看我的错误日志。即使我设置为在 8800 上收听 nginx 尝试在 80 上运行它
  • 看看ps -ef | grep nginx,您将看到实际的 conf 文件。还要检查 iptables 步骤。
  • 我能够在不向 iptables 添加规则的情况下使其正常工作。什么情况下需要iptables?
【解决方案2】:

看来问题出在一行

        listen localhost;

“listen”指令可以采用端口号、主机、 或两者。我相信在你的情况下这条线默认 端口到 80,这会导致你的问题 正在经历。

另外,如果“默认”不是唯一的文件,请注意 在 hosts_available 中。这些(每个服务器块)中的每一个都将默认 除非在那里被覆盖,否则使用端口 80。只是 在这种情况下,编辑“默认”是不够的。

【讨论】:

    【解决方案3】:

    我发现这个关于服务器故障的答案很有帮助:https://serverfault.com/questions/655067/is-it-possible-to-make-nginx-listen-to-different-ports

    您可以将端口添加到一个服务器块以保持简单:

    server {
        listen 80;
        listen 8000;
        server_name example.org;
        root /var/www/;
    }
    

    【讨论】:

    • 这行不通。问题清楚地表明端口 80 已在使用中。 @Cris P 看起来是正确的答案,尽管它没有被接受。
    猜你喜欢
    • 2012-02-12
    • 2023-03-28
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多