1. 程式人生 > 實用技巧 >Windows環境配置 Nginx

Windows環境配置 Nginx

部署

第一步

  從官網下載 Nginx 包,版本自己選擇,官網地址:http://nginx.org/

第二步

  選擇Windows版本的安裝包進行下載,根據圖片描述,最新版是 1.19.3 (具體看當前官網),穩定版是 1.18.0(具體看當前官網)

第三步

  下載後得到的exe檔案不要雙擊,用解壓程式進行解壓,得到一個資料夾,將資料夾複製到指定目錄就完成了安裝

注意:一定不要直接雙擊nginx.exe,這樣會導致修改配置後重啟、停止 nginx 無效,需要手動關閉工作管理員內的所有 nginx 程序,再啟動才可以

第四步

  啟動Nginx

# 進入 Nginx 所在目錄
cd c:\nginx
-1.xx.x # 啟動 Nginx 服務 start nginx # 執行後會有一閃而過的資訊,是正常的 # 檢視任務程序是否存在,可使用以下命令s或開啟工作管理員都行 tasklist /fi "imagename eq nginx.exe" # 日誌路徑 c:\nginx-1.xx.x\log

第五步

  瀏覽器訪問 127.0.0.1:80,檢視是否成功,看到以下頁面表示成功

文件結構

預設配置檔案路徑 -- > c:/nginx-1.xx.x/conf/nginx.conf

#user  nobody;
# ---------- 工作程序數,一般設定為cpu核心數
worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; # ---------- 最大連線數,一般設定為cpu*2048 events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; # ---------- 客戶端連結超時時間 keepalive_timeout 65; #gzip on; # ---------- 當配置多個server節點時,預設server names的快取區大小就不夠了,需要手動設定大一點 server_names_hash_bucket_size 512 # ---------- server表示虛擬主機可以理解為一個站點,可以配置多個server節點搭建多個站點,每一個請求進來確定使用哪個server由server_name確定 server { listen 80; server_name localhost; # ---------- 編碼格式,避免url引數亂碼可修改為charset utf-8 #charset koi8-r; #access_log logs/host.access.log main; # ----------location用來匹配同一域名下多個URI的訪問規則,比如動態資源如何跳轉,靜態資源如何跳轉等。location後面跟著的/代表匹配規則 location / { # 站點根目錄,可以是相對路徑,也可以使絕對路徑 root html; index index.html index.htm; # 預設主頁 } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; # 轉發後端站點地址,一般用於做軟負載,輪詢後端伺服器 #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
nginx.conf

注意事項

  1. Nginx預設埠號為 80,若已被佔用則無法啟動,可在配置檔案中修改埠號,修改陪之後可執行命令檢查配置是否合法
    nginx -t -c /nginx-1.xx.x/conf/nginx.conf

  2. Nginx 資料夾路徑不要包含中文