目錄
- 說明
- 下載
- 安裝
- 1 準(zhǔn)備:
- 2 運(yùn)行Nginx:
- 使用
- 使用注意事項(xiàng):
說明
Nginx (engine x) 是一個高性能的HTTP和反向代理服務(wù)器,也是一個IMAP/POP3/SMTP服務(wù)器。源代碼以類BSD許可證的形式發(fā)布,因它的穩(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名。在連接高并發(fā)的情況下,Nginx是Apache服務(wù)器不錯的替代品:Nginx在美國是做虛擬主機(jī)生意的老板們經(jīng)常選擇的軟件平臺之一。能夠支持高達(dá) 50,000 個并發(fā)連接數(shù)的響應(yīng)。筆者將會使用Nginx將默認(rèn)網(wǎng)址使用的80端口與Tomcat使用的8080端口進(jìn)行對接,實(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)上述提示說明啟動nginx失敗,是由于電腦的80端口已經(jīng)被占用,占用80端口的原因和解除方式都有很多種,例如SQLServer的ReportingServicesService占用,Apache,IIS,或者其他原因,筆者在這就不說明怎么解除占用了
解除占用后正常啟動如下圖:可以在任務(wù)管理器看到有兩個nginx程序在運(yùn)行,至于為什么是兩個,可以查看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í)看到瀏覽器上的安全兩個綠字沒有,可以用https裝13了,https最多的還是用于表單提交,一般情況還是不要用,增加服務(wù)器壓力,但是貌似ios應(yīng)用貌似要求必須用https了,這里配置好https,就可以用來寫帶有https的servlet了)
使用注意事項(xiàng):
1 首次安裝Nginx時(shí),不要直接點(diǎn)擊nginx.exe程序,否則會導(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配置不正確會導(dǎo)致訪問網(wǎng)頁時(shí)樣式文件(js、css)不能一起返回,經(jīng)過測試,筆者的配置是沒有這個問題的