簡體   English   中英

如何在 80 以外的其他端口上運行 Nginx

[英]How to run Nginx on a different port other than 80

我無法運行 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 - 高性能網絡服務器和反向代理服務器加載:加載(/lib/systemd/system/nginx.service;啟用;供應商預設:啟用)
活動:自美國東部時間周五 2019-04-26 04:23:14 以來失敗(結果:退出代碼); 13分鍾前
文檔:man:nginx(8)
進程:16955 ExecStart=/usr/sbin/nginx -g daemon on; master_process 開啟; (代碼=退出,狀態=1/失敗)

進程:16944 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process 開啟; (代碼=退出,狀態=0/成功)
主 PID:80941(代碼=退出,狀態=0/成功)
4 月 26 日 04:23:11 ubuntu systemd[1]:啟動高性能 Web 服務器和反向代理服務器...
4 月 26 日 04:23:11 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)
4 月 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:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
4 月 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] 仍然無法綁定()
4 月 26 日 04:23:14 ubuntu systemd[1]:nginx.service:控制進程退出,代碼=退出狀態=1
4 月 26 日 04:23:14 ubuntu systemd[1]:nginx.service:失敗,結果為“退出代碼”。
4 月 26 日 04:23:14 ubuntu systemd[1]:無法啟動高性能 Web 服務器和反向代理服務器。

你必須去/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;

看起來問題出在線路上

        listen localhost;

“listen”指令可以采用端口號、主機或兩者。 我相信在您的情況下,此行將端口默認為 80,這會導致您遇到問題。

此外,請注意“默認”是否不是 hosts_available 中的唯一文件。 這些(每個服務器塊)中的每一個都將默認使用端口 80,除非在那里被覆蓋。 在這種情況下,僅僅編輯“默認”是不夠的。

我發現這個關於服務器故障的答案很有幫助: 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/;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM