[英]How to start nginx via different port(other than 80)
嗨,我是 nginx 的新手,我嘗試在我的服務器(運行 Ubuntu 4)上設置它,該服務器已經運行了 apache。
所以在我apt-get install
它之后,我嘗試啟動 nginx。 然后我收到這樣的消息:
Starting nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
這是有道理的,因為 Apache 正在使用端口 80。
然后我嘗試修改nginx.conf
,我參考了一些文章,所以我改成這樣:
server {
listen 8080;
location / {
proxy_pass http://xx.xx.xx.xx:9500;
proxy_set_header Host $host:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
保存並嘗試再次啟動 nginx 后,我仍然遇到與以前相同的錯誤。 我真的找不到關於此的相關帖子,有沒有好心人可以提供一些線索?
提前致謝 :)
================================================== ========================
我應該在這里發布 conf 中的所有內容:
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 81;
location / {
proxy_pass http://94.143.9.34:9500;
proxy_set_header Host $host:81;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
}
}
mail {
See sample authentication script at:
http://wiki.nginx.org/NginxImapAuthenticateWithApachePhpScript
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;
}
}
基本上,除了添加服務器部分之外,我什么都沒做。
您必須轉到/etc/nginx/sites-enabled/
,如果這是默認配置,則應該有一個文件名: default
。
通過定義所需的端口來編輯該文件; 在下面的代碼片段中,我們在端口 81 上為 Nginx 實例提供服務。
server {
listen 81;
}
要啟動服務器,請運行下面的命令行;
sudo service nginx start
您現在可以在端口 81 上訪問您的應用程序(對於 localhost, http://localhost:81 )。
您需要更改 Apache 或 Nginx 的配置端口。 執行此操作后,您將需要使用您使用的“服務”命令重新啟動重新配置的服務器。
阿帕奇
編輯
sudo subl /etc/apache2/ports.conf
並將以下行中的 80 更改為不同的內容:
Listen 80
如果您只是在此處更改端口或添加更多端口,則可能還需要更改中的 VirtualHost 語句
sudo subl /etc/apache2/sites-enabled/000-default.conf
並將以下行中的 80 更改為不同的內容:
<VirtualHost *:80>
然后通過以下方式重新啟動:
sudo service apache2 restart
nginx
編輯
/etc/nginx/sites-enabled/default
並更改以下行中的 80 :
listen 80;
然后通過以下方式重新啟動:
sudo service nginx restart
按照這個:打開你的配置文件
vi /etc/nginx/conf.d/default.conf
更改您正在偵聽的端口號;
listen 81;
server_name localhost;
向 iptables 添加規則
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 81 -j ACCEPT
重啟IPtables
service iptables restart;
重啟nginx服務器
service nginx restart
在端口 81 上訪問 nginx 服務器文件
如果您在 Windows 上,則在<nginx 安裝路徑 >/conf文件夾中的文件nginx.conf中存在以下端口相關的服務器設置。
server {
listen 80;
server_name localhost;
....
更改端口號並重啟實例。
如果您在使用 Docker 時遇到此問題,請務必映射正確的端口號。 如果您在運行 docker 時(或通過 docker-compose.yml)映射端口 81:80,則您的 nginx 必須偵聽端口 80而不是81,因為 docker 已經進行了映射。
我自己在這個問題上花了很多時間,所以希望它可以對未來的谷歌員工有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.