nginx作为web服务应用
Nginx 是一款輕量級的 Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,其特點(diǎn)是占有內(nèi)存少,并發(fā)能力強(qiáng)。我的應(yīng)用主要有3個,端口轉(zhuǎn)發(fā)、反向代理和web服務(wù)。我的網(wǎng)絡(luò)架構(gòu)是,所有訪問都指向nginx服務(wù)器,由它根據(jù)端口和地址轉(zhuǎn)發(fā)至不同的服務(wù)器。? 默認(rèn)環(huán)境是debain系統(tǒng),已安裝nginx。
1、資源準(zhǔn)備:
中文官網(wǎng):??Nginx中文文檔
官網(wǎng)下載:nginx: download
2、nginx配置文件
在/etc/nginx中,有三個文件 nginx.conf,sites-enabled,sites-available。主配置文件 nginx.conf,由于該文件 #include sites-enabled,所以有兩種方式擴(kuò)展
1)可在sites-enabled文件夾下,新增配置文件;
2)在sites-available文件夾下,新增配置文件,再將文件通過軟鏈接,到sites-enabled文件夾下。
3、端口轉(zhuǎn)發(fā):
server {listen 80;#訪問的地址 http://192.168.0.1:80/text/...location /text/ {proxy_pass http://192.168.0.2:8080/text/;}}4、反向代理及負(fù)載均衡:
server {listen 80;##訪問的地址 http://192.168.0.1:80/text/...location /text/ {proxy_pass http://myText/text/;proxy_next_upstream error http_404 http_500 http_502 http_503 http_504;proxy_connect_timeout 20s;proxy_read_timeout 20s;proxy_send_timeout 20s;} }此處myText在nginx.conf配置,由于192.168.0.2的權(quán)重是3,優(yōu)先訪問該服務(wù)器,若服務(wù)器的失敗次數(shù)大于5,則切換至192.168.0.3的服務(wù)器。實(shí)現(xiàn)一定程度的負(fù)載均衡和容災(zāi)。
#weight: 權(quán)重,數(shù)值越大,流量越多 #max_fails:最大訪問失敗次數(shù) #fail_timeout:訪問失敗等待時長http {upstream myText{server 192.168.0.2:8081 weight=3 max_fails=5 fail_timeout=60s;server 192.168.0.3:8081 max_fails=5 fail_timeout=60s;} }5、web服務(wù)
server {listen 80;##訪問的地址 http://192.168.0.1:80/text/inde.html,Nginx訪問服務(wù)器## /usr/local/html/text/templates/web/index.htmllocation /text/ {alias /usr/local/html/text/templates/web/;} }6、操作nginx
啟動nginx:systemctl start nginx
查看狀態(tài):systemctl status nginx
停止nginx:systemctl stop nginx
重啟nginx:systemctl?restart nginx
重載當(dāng)配置更改后,無須重啟nginx服務(wù)器:nginx -s reload
7、查看日志:
日志文件的路徑:?/var/log/nginx/
包含access.log和 error.log?兩個文件,查看訪問記錄和異常日志,其中access.log文件,在應(yīng)用中指數(shù)級增長,建議新建一個.sh腳本,定時清理并保存近一周的記錄。
#!/bin/bash #mycat的日志清理logs_path="/usr/local/mycat/logs/" find $logs_path -name `date -d "-1 month" +%Y-%m` -exec rm -rf {} \;linux終端,輸入 crontab -e?
# 每天2點(diǎn)清除mycat日志 0 2 ?* ?* ?* ?sh ?/usr/local/bin/mycat_clean.sh
?
總結(jié)
以上是生活随笔為你收集整理的nginx作为web服务应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是Linux CNC
- 下一篇: Ipad平替电容笔哪个品牌口碑好?app