一.介紹安裝
公司由于linux云服務器還沒批下來,暫時先在windows服務器上測驗,Windows版nginx使用本地Win32 API(而非Cygwin仿真層),當前僅使用select()和poll()(1.15.9)連接處理方法(事件驅動模型),因此不應期望高性能和可伸縮性(在linux上支持epoll,它是性能最高的一種),由于這個原因和其他一些已知問題,適用于Windows的Nginx版本被認為是Beta版本,目前,除了XSLT過濾器,影像過濾器,GeoIP模塊和嵌入式Perl語言之外,它提供的功能幾乎與UNIX版本的nginx相同,
1.1下載安裝
windows下載安裝包1.17.7
http://nginx.org/en/download.html
啟動nginx, 沒有找到nginx.exe行程,
C:\Users\Administrator>cd C:\nginx-1.17.7 C:\nginx-1.17.7>start nginx
查看日志C:\nginx-1.17.7\logs
2020/01/03 13:55:53 [emerg] 11992#35328: 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.2 查看誰占用了80埠
C:\nginx-1.17.7>netstat -ano|findstr "80"
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 4

可能是http,查看下http服務狀態:
C:\nginx-1.17.7>netsh http show servicestate
發現6468占用了,再查看任務管理器,是IIS占用了,如下所示:

1.3 解決windows 80埠被iis占用
方法1,啟動欄輸入regedit
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTP
將start 的3改為0
重啟服務器,
方法2:直接關閉iis中的默認站點 Default Web Site
再次啟動 查看行程狀態 tasklist /fi "imagename eq nginx.exe"

一個是主行程(守護行程),另一個作業行程
訪問http://127.0.0.1/,查看nginx首頁

1.4 nginx 命令管理
nginx -s stop 快速結束服務
nginx -s quit 正常結束服務
nginx -s reload 更改配置,使用新配置啟動新作業行程,正常關閉舊作業行程
nginx -s reopen 重新打開日志檔案
Nginx/Win32是運行在一個控制臺程式,而非windows服務方式的,
1.5其它事項
日志檔案,如果是debug級別,日志檔案會增長的比較快,需要更大的磁盤目錄空間,
二. windows下nginx.conf配置示例
#默認所有用戶都能啟動nginx行程 #user nobody; #worker行程數,配置對應cpu核心數 worker_processes 1; #服務器錯誤日志 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #主行程,也叫守護行程pid存放路徑 #pid logs/nginx.pid; #有默認的事件驅動模型 events { #每個worker行程的最大連接數 worker_connections 1024; accept_mutex on; #默認為on,解決"驚群"問題 } http { #識別web資源型別,引入外部 mime.types檔案,路徑在conf/mime.types下 include mime.types; #默認資源型別 default_type application/octet-stream; #自定義服務日志,這里記錄前端請求的日志,而不是全域中nginx行程運行的常規日志 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #訪問日志,main是log_format格式字串 #access_log logs/access.log main; #方式轉輸方式,轉輸的資料最大量不能超過sendfile_max_chunk 128k sendfile on; #tcp_nopush on; #設定格式 charset utf-8; #配置連接超時時間 #keepalive_timeout 0; keepalive_timeout 65; #默認開啟gzip壓縮 #gzip on; # 快取設定,基于proxy_cache機制 #path快取目錄 #levels相對于path指定目錄的第幾級hash目錄中快取資料檔案,目錄層級2級, #keys_zone索引記憶體區域名稱 10m是大小 #max_size硬碟中快取資料的大小限制(超過啟動nginx自己的淘汰規則), #inactive在60分鐘的時間內沒有被訪問就會被清理,存放臨時檔案 #use_temp_path 關閉 proxy_cache_path cache levels=1:2 keys_zone=ABC_cache:10m max_size=5g inactive=60m use_temp_path=off; # HTTP server 監聽 www.ABC.com server { listen 80; #監聽埠 server_name www.ABC.com; #創建獨立的日志,重點:手動在logs下創建www.ABC.com檔案夾 access_log logs/www.ABC.com/access.log; error_log logs/www.ABC.com/error.log; #charset koi8-r; location / { #root html; #index index.html index.htm; #快取設定 proxy_cache ABC_cache; proxy_cache_methods GET HEAD POST; proxy_cache_valid 200 304 60m; #200和304頭資訊過期時間 proxy_cache_valid 404 1m; proxy_cache_valid any 10m; #其他過期時間10分鐘 expires 2h; proxy_pass http://ABCservers; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { #目錄是本機web服務器靜態資源目錄 root D:\\www\www.ABC.com\wwwroot; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # HTTPS server 監聽apicms.ABC.com埠 server { listen 443 ssl; server_name apicms.ABC.com; #創建獨立的日志,重點:手動在logs下創建apicms.ABC.com檔案夾 access_log logs/apicms.ABC.com/access.log; error_log logs/apicms.ABC.com/error.log; ssl_certificate ./ssl/apicms.ABC.com.crt; ssl_certificate_key ./ssl/apicms.ABC.com.rsa; 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; proxy_pass https://apicmsservers; proxy_set_header Connection keep-alive; proxy_set_header Host $host; #web服務器端獲得用戶的真實ip request.getAttribute("X-Real-IP") proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-Port $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } #apicms后端服務器組,如果一個服務器處理請求出錯,會順次交給組內下一個服務器進行處理,以此類推 upstream apicmsservers{ #當某個server請求二次失敗后,10分鐘以不會把請求發往已檢查出標記為不可用的服務器 server 127.0.0.1:44301 max_fails=2 fail_timeout=600s weight=5; server 192.168.0.125:44301 max_fails=2 fail_timeout=600s weight=5; } #官網后端服務器組 upstream ABCservers{ server 127.0.0.1:8081 max_fails=2 fail_timeout=600s weight=5; server 192.168.0.125:8081 max_fails=2 fail_timeout=600s weight=5; } }
(1) https協議需要證書, 將申請的.pfx證書使用openssl工具,將轉換成crt和rsa檔案,再放在nginx目錄的ssl檔案夾下,轉換示例如下:
openssl >pkcs12 -in D:\SSL\1717704_apicms.ABC.com.pfx -clcerts -nokeys -out D:\SSL\apicms.ABC.com.crt
openssl >pkcs12 -in D:\SSL\1717704_apicms.ABC.com.pfx -nocerts -nodes -out D:\SSL\apicms.ABC.com.rsa
(2)配置中所有相對路徑,都是nginx目錄下的, 比如:logs/ , proxy_cache_path cache , ./ssl
(3) 如果沒有購買域名系結當前nginx服務器ip, 那么測驗需要在nginx服務器上使用hosts檔案,才能監聽server塊的虛擬主機名,hosts系結如下所示:
127.0.0.1 apicms.ABC.com
127.0.0.1 www.ABC.com
再ping下確認,hosts檔案是否生效

參考資料
http://nginx.org/en/docs/windows.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/13737.html
標籤:Windows
上一篇:windows2016_x64搭建ELK(datasource->filebeat->logstash->elasticsearch->kibana)
下一篇:急求
