2012-05-31 37 views
96

嗨,我是nginx上的新手,我試圖在我的服務器上運行它(已運行Ubuntu 4),它已經運行了apache。如何通過不同的端口啓動nginx(80以外)

所以在我之後,我試着啓動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; 
    } 
} 

基本上,除了添加服務器部分,我什麼也沒有改變。

+0

您是否有_same_消息。可能是端口已經改變爲另一個?你嘗試通過sudo運行nginx嗎? – dmr

+0

對於centos,這個鏈接可能有幫助http://serverfault.com/questions/566317/nginx-no-permission-to-bind-port-8090-but-it-binds-to-80-and-8080 – vintuwei

回答

130

你必須去/ etc/nginx/sites-enabled /,如果這是默認配置,那麼應該有一個名爲「default」的文件。

編輯該文件,並把(如果你願意把81作爲端口的nginx)

服務器{ 聽81; }

的啓動服務器

須藤服務nginx的啓動

然後訪問本地主機:81

+8

+1提到需要真正啓動nginx服務。它不是很明顯,它必須完成。 – vbocan

+0

或者,如果您沒有安裝服務(通常爲OS X),您可以運行: 'sudo nginx -s reload' –

+0

如果nginx已經在運行'sudo service nginx restart' – depicus

15

您需要更改Apache或Nginx的配置端口。執行此操作後,您需要使用您使用的'service'命令來重新啓動重新配置的服務器。


阿帕奇

編輯

sudo subl /etc/apache2/ports.conf 

,並在下一行改變80到不同的東西:

Listen 80 

如果你只是更改端口或添加更多端口在這裏,你可能還需要更改V irtualHost在

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 
+0

非常滿意你的編輯imsheth。 TNX。 –

13

關注這個: 打開你的配置文件

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

012訪問年nginx的服務器上的文件
+0

我的問題是使用這種方法解決的,它幾乎和你類似。 – Shivam

+0

http://stackoverflow.com/users/3710715/shivam爲什麼你不標記其中一個答案作爲答案;-) –

+0

因爲我沒有問這個問題。所以,我猜不出任何答案爲答案:) – Shivam

相關問題