nginx for windows是一款輕量級的Web 服務器,現在很多反向代理服務器及電子郵件(IMAP/POP3)代理服務器,不僅可以減小服務器壓力,同時也提高安全度。
關于Nginx
Nginx是Apache服務器不錯的替代品:Nginx在美國是做虛擬主機生意的老板們經常選擇的軟件平臺之一。能夠支持高達 50,000 個并發連接數的響應。筆者將會使用Nginx將默認網址使用的80端口與tomcat使用的8080端口進行對接,實現使用80端口(域名)訪問Tomcat下的網頁,并配置HTTPS協議提高安全性。
安裝提示
1、安裝的說明
下載后解壓得到如下一些文件

首先需要域名和SSL證書來配置HTTPS協議,SSL證書可以從很多地方獲取或者自己創建
這里以騰訊云的CA證書為例子,有了域名和SSL證書后,按照騰訊云的官方提示,將配置Nginx的安全證書(.crt)和注冊表項(.key)放到Nginx解壓目錄下的conf文件夾下方便管理【證書的名字這里改為1.crt和2.key】

然后需要將其配置到Nginx中,修改conf目錄下的nginx.conf文件如下:
#user? nobody;
#user root;
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;
#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;
charset utf-8;
#keepalive_timeout? 0;
keepalive_timeout? 120;
#gzip? on;
#填寫自己的服務器ip和Tomcat端口
upstream local_tomcat {
server xxx.xxx.xxx.xxx:8080;
}
server {
listen 80 default_server;
listen 443 ssl;
charset utf-8;
#填寫自己的網站域名
server_name? www.xxxx.xxx;
#證書文件
ssl_certificate C:/nginx-1.12.2/conf/1.crt;
#私鑰文件
ssl_certificate_key C:/nginx-1.12.2/conf/2.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
#配置HTTPS?
location ~ /^[H,h][T,t][T,t][P,p][S,s]/ {
#網站根目錄,為Tomcat下的wepapps目錄
root C:/Tomcat7/apache-tomcat-7.0.82/webapps;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.jsp$ {
root C:/Tomcat7/apache-tomcat-7.0.82/webapps;
proxy_pass http://local_tomcat;
proxy_set_header Host $host:$server_port;
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;
}
location ~ \.html$ {
root C:/Tomcat7/apache-tomcat-7.0.82/webapps;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root C:/Tomcat7/apache-tomcat-7.0.82/webapps;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
#? ? }
#}
}
2、 運行Nginx:
打開命令提示符跳轉到nginx解壓目錄輸入nginx

出現上述提示說明啟動nginx失敗,是由于電腦的80端口已經被占用,占用80端口的原因和解除方式都有很多種,例如SQLServer的ReportingServicesService占用,Apache,iis,或者其他原因,筆者在這就不說明怎么解除占用了
解除占用后正常啟動如下圖:可以在任務管理器看到有兩個nginx程序在運行,至于為什么是兩個,可以查看Nginx官方的文檔,不解釋了

3、關于使用
開啟Nginx,Tomcat。打開瀏覽器輸入http(s)://你的域名/項目文件名/文件名即可進行訪問
例如筆者配置的服務器(如果我的服務器開著的話可以訪問。。。):

使用注意事項:
1 首次安裝Nginx時,不要直接點擊nginx.exe程序,否則會導致很多問題,當配置完成后,以后再開啟nginx即可直接點擊nginx.exe程序,不需要再使用命令提示符操作,附nginx的基本操作指令:
開啟:start nginx
檢查配置文件:nginx -t -c C:/nginx-1.12.2/conf/nginx.conf
重新加載配置文件(很實用的指令):nginx -s reload
快速停止:nginx -s stop
完整停止:nginx -s quit
2 檢測配置文件沒有問題,但是使用HTTPS不能訪問,可能是由于防火墻的原因,可以將其關閉試試,成功后,可以自己配置防火墻入網規則,將80(Nginx),443(SSL),1433(sql server),8080(Tomcat)等等端口添加至防火墻里,來繼續開啟防火墻(我當時就是在這麻煩了很久)
3 有些nginx.conf配置不正確會導致訪問網頁時樣式文件(js、css)不能一起返回,經過測試,筆者的配置是沒有這個問題的
ps:感謝作者:蕓靈fly分享的教程
更新日志:
v1.19.9版本更新
修復 nginx 在使用郵件代理模塊 (mail proxy module) 時無法構建的問題,使用 ngx_mail_ssl_module 則正常。這個錯誤出現在 1.19.8 中
修復當與 gRPC 后端搭配使用時,可能出現"upstream sent response body larger than indicated content length"錯誤。這個問題出現在 1.19.1 中
如果客戶端在丟棄請求體的同時關閉了連接,nginx 可能在 keepalive 超時前不會關閉連接
當等待 auth_delay 或 limit_req 延遲時,或者與后端一起搭配使用時,nginx 可能無法檢測到客戶端已經關閉的連接
修復 eventport 方法中的錯誤
v1.2.8版本更新
新的會話沒有如果“ssl_session_cache共享”指令用于存儲和共享內存中有沒有免費的空間。感謝彼得·西科拉。
如果子請求使用一個DNS子請求處理過程中發生了錯誤的反應可能會掛起。感謝到Lanshun周。
在的ngx_http_mp4_module。由于赫爾諾特Vormayr。 *)修正:在后端使用的計數。
v1.2.12版本更新
變量支持在“proxy_bind”,“fastcgi_bind”,“memcached_bind”,“scgi_bind”,和“uwsgi_bind”指令。
管,request_length,$ time_iso8601,美元和$time_local變量,現在不僅可以在“log_format”使用指令。
支持IPv6ngx_http_geoip_module。
修正指令“proxy_method”。
修正一個分割故障可能發生在工作進程中,如果使用的投票方法解析。
修正nginx的可能霸占CPU在SSL握手與后端的選擇,投票,或使用/ dev / poll的方法。
修正“暴擊SSL_write()失敗(SSL:)”的錯誤。
修正“client_body_in_file_only”指令的錯誤。
修正指令“fastcgi_keep_conn”。
v1.2.2版本更新
修復了使用 try_files 時可能導致的段錯誤的問題,該問題出現在 1.1.19 版本
當緩沖超過 IOV_MAX 時可能會導致響應被截斷
修復了 image_filter 指令使用 crop 參數的問題



