Nginx安裝配置 -反向代理實現端口轉換

一、bind() to 0.0.0.0:80 failed
  原因:
        是由於Windows10系統默認把80端口給佔用了,而nginx的端口也是80,所以說報錯。

2018/10/27 05:40:15 [emerg] 21724#22312: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
2018/10/27 05:41:54 [emerg] 15196#14864: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
解決:
1、運行中輸入regedit進入註冊表;
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTP

找到start,原本是3,我這裏改成了4.確認之後重啓系統。


二、CreateFile() "D:\Program Files (x86)\nginx-1.14.0/logs/nginx.pid" failed (2: The system cannot find the file specified)
       正常情況下D:\Program Files (x86)\nginx-1.14.0/logs/nginx.pid,logs目錄下會有nginx.pid文件,也有可能該目錄下沒有這個文件。如果沒有,創建一個空文件。參考:windows系統nginx重啓發生異常The system cannot find the file specified

2018/10/27 05:44:47 [error] 7336#12764: CreateFile() "D:\Program Files (x86)\nginx-1.14.0/logs/nginx.pid" failed (2: The system cannot find the file specified)
三、Nginx命令
啓動:start nginx.exe

停止:nginx.exe -s stop

重新加載:nginx.exe -s reload

四、nginx.conf主配置文件
 
#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
   
    keepalive_timeout  65;
 
    gzip  on;
    server {
        listen       80;
        server_name  manage.leyou.com;
 
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
        location / {
            proxy_pass http://127.0.0.1:9001;
            proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }
    }
    server {
        listen       80;
        server_name  api.leyou.com;
 
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
        location / {
            proxy_pass http://127.0.0.1:10010;
            proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }
    }
}
五、start nginx.exe啓動命令


六、web項目路徑是localhost:9001

瀏覽器輸入命令:localhost:9001

ps:如果每次都去改host文件的話,比較麻煩,當然也可以實現。這裏推薦使用SwitchHosts。

點擊ok之後,再去瀏覽器輸入域名即可訪問了。


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章