Nginx在Windows下的安裝與使用過程詳解
說明
Nginx (engine x) 是一個(gè)高性能的HTTP和反向代理服務(wù)器,也是一個(gè)IMAP/POP3/SMTP服務(wù)器。源代碼以類BSD許可證的形式發(fā)布,因它的穩(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名。在連接高并發(fā)的情況下,Nginx是Apache服務(wù)器不錯(cuò)的替代品:Nginx在美國是做虛擬主機(jī)生意的老板們經(jīng)常選擇的軟件平臺(tái)之一。能夠支持高達(dá) 50,000 個(gè)并發(fā)連接數(shù)的響應(yīng)。筆者將會(huì)使用Nginx將默認(rèn)網(wǎng)址使用的80端口與Tomcat使用的8080端口進(jìn)行對(duì)接,實(shí)現(xiàn)使用80端口(域名)訪問Tomcat下的網(wǎng)頁,并配置HTTPS協(xié)議提高安全性。
下載
官網(wǎng):http://nginx.org/en/download.html
選擇合適的Windows版本進(jìn)行下載,筆者以穩(wěn)定版1.12.2版本為例,解壓后如下圖:
安裝
1 準(zhǔn)備:
首先需要域名和SSL證書來配置HTTPS協(xié)議,SSL證書可以從很多地方獲取或者自己創(chuàng)建,筆者以騰訊云的CA證書為例:
有了域名和SSL證書后,按照騰訊云的官方提示,將配置Nginx的安全證書(.crt)和注冊表項(xiàng)(.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; #填寫自己的服務(wù)器ip和Tomcat端口 upstream local_tomcat { server xxx.xxx.xxx.xxx:8080; } server { listen 80 default_server; listen 443 ssl; charset utf-8; #填寫自己的網(wǎng)站域名 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]/ { #網(wǎng)站根目錄,為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 運(yùn)行Nginx:
打開命令提示符跳轉(zhuǎn)到nginx解壓目錄輸入nginx
出現(xiàn)上述提示說明啟動(dòng)nginx失敗,是由于電腦的80端口已經(jīng)被占用,占用80端口的原因和解除方式都有很多種,例如SQLServer的ReportingServicesService占用,Apache,IIS,或者其他原因,筆者在這就不說明怎么解除占用了
解除占用后正常啟動(dòng)如下圖:可以在任務(wù)管理器看到有兩個(gè)nginx程序在運(yùn)行,至于為什么是兩個(gè),可以查看Nginx官方的文檔,不解釋了
使用
開啟Nginx,Tomcat。打開瀏覽器輸入http(s)://你的域名/項(xiàng)目文件名/文件名即可進(jìn)行訪問
例如筆者配置的服務(wù)器(如果我的服務(wù)器開著的話可以訪問。。。):
HTTPS:https://www.yunlingfly.cn/HelloNginx/index.html
HTTP:http://www.yunlingfly.cn/HelloNginx/index.jsp
(注意用https時(shí)看到瀏覽器上的安全兩個(gè)綠字沒有,可以用https裝13了,https最多的還是用于表單提交,一般情況還是不要用,增加服務(wù)器壓力,但是貌似ios應(yīng)用貌似要求必須用https了,這里配置好https,就可以用來寫帶有https的servlet了)
使用注意事項(xiàng):
1 首次安裝Nginx時(shí),不要直接點(diǎn)擊nginx.exe程序,否則會(huì)導(dǎo)致很多問題,當(dāng)配置完成后,以后再開啟nginx即可直接點(diǎn)擊nginx.exe程序,不需要再使用命令提示符操作,附nginx的基本操作指令:
開啟:start nginx
檢查配置文件:nginx -t -c C:/nginx-1.12.2/conf/nginx.conf
重新加載配置文件(很實(shí)用的指令):nginx -s reload
快速停止:nginx -s stop
完整停止:nginx -s quit
2 檢測配置文件沒有問題,但是使用HTTPS不能訪問,可能是由于防火墻的原因,可以將其關(guān)閉試試,成功后,可以自己配置防火墻入網(wǎng)規(guī)則,將80(Nginx),443(SSL),1433(SQL Server),8080(Tomcat)等等端口添加至防火墻里,來繼續(xù)開啟防火墻(我當(dāng)時(shí)就是在這麻煩了很久)
3 有些nginx.conf配置不正確會(huì)導(dǎo)致訪問網(wǎng)頁時(shí)樣式文件(js、css)不能一起返回,經(jīng)過測試,筆者的配置是沒有這個(gè)問題的
到此這篇關(guān)于Nginx在Windows下的安裝與使用的文章就介紹到這了,更多相關(guān)Windows安裝使用Nginx內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx 關(guān)閉默認(rèn)站點(diǎn)的方法
國內(nèi)機(jī)房一般都要求網(wǎng)站主關(guān)閉空主機(jī)頭,防止未備案的域名指向過來造成麻煩2012-09-09Nginx反向代理在Web應(yīng)用中的實(shí)戰(zhàn)分享
本文將介紹Nginx反向代理的基本原理和配置,以及如何利用Nginx實(shí)現(xiàn)高可用性和故障轉(zhuǎn)移,最后,我們將探討如何監(jiān)控Nginx反向代理的性能并進(jìn)行日志分析,需要的朋友可以參考下2024-08-08解析阿里云centos7服務(wù)器nginx配置及常見問題解答
這篇文章主要介紹了阿里云centos7服務(wù)器nginx配置及常見問題解答,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07nginx?status配置及參數(shù)配置小結(jié)
本文主要介紹了nginx?status配置及參數(shù)配置,其實(shí)要監(jiān)控Nginx的狀態(tài)非常簡單,它內(nèi)建了一個(gè)狀態(tài)頁,只需修改Nginx配置啟用Status即可,感興趣的可以了解一下2024-04-04Nginx實(shí)現(xiàn)不同域名輸出不同的服務(wù)器頭信息方法
這篇文章主要介紹了Nginx實(shí)現(xiàn)不同域名輸出不同的服務(wù)器頭信息方法,本文使用了一個(gè)ngx_headers_more模塊實(shí)現(xiàn)這個(gè)特殊需求,需要的朋友可以參考下2015-02-02