Linux之nginx入门
?詳見可參考:https://www.cnblogs.com/tiger666/p/10239307.html?tdsourcetag=s_pctim_aiomsg
1. 常用的WEB框架有哪些:
django 重量級別的框架,功能大而全, form表單,ORM, 內(nèi)置的模塊非常多 600-2000req/s
flask 輕量級的框架, 從第三方引入過來的 2500req/s
tornado(沒學(xué)過) 異步非阻塞 支持多用戶并發(fā)訪問3000req/s
sanic 是python3.5之后的一個框架, 20000req/s
?
我們的WEB框架是用來處理用戶請求,獲取數(shù)據(jù)并返回給用戶查看
在上面開發(fā)應(yīng)用程序用的
?
2. web服務(wù)器
是用來對外提供服務(wù)的, www服務(wù)
IIS
apache 非常普通的WEB服務(wù)器 對高并發(fā)沒有太大的支持
nginx 開源的,支持高并發(fā),高性能的服務(wù)器
tengine 淘寶自己的nginx服務(wù)器,其實它的配置和nginx一樣
?
面試回答nginx技巧
支持高并發(fā),能支持幾萬并發(fā)連接資源消耗少,在3萬并發(fā)連接下開啟10個nginx線程消耗的內(nèi)存不到200M
可以做http反向代理和負載均衡
支持異步網(wǎng)絡(luò)i/o事件模型epoll
?
linux下測試訪問網(wǎng)站命令
curl -i 域名 # 訪問網(wǎng)站并返回網(wǎng)站內(nèi)容(源代碼) curl -I 域名 # 返回網(wǎng)站的服務(wù)器信息3. nginx編譯安裝
1 安裝所需要的依賴庫yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel 2 下載nginx安裝源碼包 wget -c https://nginx.org/download/nginx-1.12.0.tar.gz3.解壓縮源碼 tar -zxvf nginx-1.12.0.tar.gz4.配置,編譯安裝./configure --prefix=/opt/nginx112make && make install 5.啟動nginx,進入sbin目錄,找到nginx啟動命令cd /opt/nginx112/sbin ./nginx #啟動 ./nginx -s stop #關(guān)閉 ./nginx -s reload # 平滑重啟 ,修改了nginx.conf之后,可以不重啟服務(wù),加載新的配置 或者 /opt/nginx112/sbin/nginx -s reload # 絕對路徑平滑重啟6 nginx的目錄結(jié)構(gòu)
?
7 nginx配置文件詳解
#定義nginx工作進程數(shù) worker_processes 5; #錯誤日志 #error_log logs/error.log; #http定義代碼主區(qū)域 http {include mime.types;default_type application/octet-stream;#定義nginx的訪問日志功能#nginx會有一個accses.log功能,查看用戶訪問的記錄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;keepalive_timeout 65;#開啟gzip壓縮傳輸 gzip on;#虛擬主機1 定義一個 斗魚網(wǎng)站 server {#定義nginx的訪問入口端口,訪問地址是 192.168.11.37:80listen 80;#定義網(wǎng)站的域名www.woshidouyu.tv#如果沒有域名,就填寫服務(wù)器的ip地址 192.168.11.37 server_name www.woshidouyu.tv;#nginx的url域名匹配#只要請求來自于www.woshidouyu.tv/111111111#只要請求來自于www.woshidouyu.tv/qweqwewqe#最低級的匹配,只要來自于www.woshidouyu.tv這個域名,都會走到這個locationlocation / {#這個root參數(shù),也是關(guān)鍵字,定義網(wǎng)頁的根目錄#以nginx安裝的目錄為相對路徑 /opt/nginx112/html #可以自由修改這個root定義的網(wǎng)頁根目錄 root html;#index參數(shù)定義網(wǎng)站的首頁文件名,默認的文件名 index index.html index.htm;}#錯誤頁面的優(yōu)化(只要是遇到前面4系列的錯誤,就會直接跳轉(zhuǎn)到相對目錄下的40x.html頁面)error_page 400 401 402 403 404 /40x.html;} } View Code
?
8 跑一個斗魚網(wǎng)站出來
修改自己本地的host文件 路徑如下:C:\Windows\System32\drivers\etc server {listen 80;server_name www.qishi2douyu.com;#access_log logs/host.access.log main;location / {root /opt/qishi2douyu/;index index.html index.htm;} ?#error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} ??
4. nginx多虛擬主機的配置
1 在192.168.12.56服務(wù)器上,跑3個網(wǎng)站出來(需要在本地文件host中添加相應(yīng)的IP地址)
www.qishi2douyu.com
www.qishi2huya.com
www.qishi2jd.com
配置文件如下:
server {listen 80;server_name www.qishi2douyu.com;#access_log logs/host.access.log main;location / {root /opt/qishi2douyu/;index index.html index.htm;} ?#error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 80;server_name www.qishi2huya.com;location / {root /opt/qishi2huya/;index index.html index.htm;} ?}server {listen 80;server_name www.qishi2jd.com;location / {root /opt/qishi2jd/;index index.html index.htm;} ?} ?
2 分別在/opt目錄下創(chuàng)建qishi2douyu、qishi2huya、qishi2jd這三個目錄
分別在目錄下創(chuàng)建index.html
3 平滑重啟nginx
/opt/nginx112/sbin/nginx -s reload?
?
nginx錯誤頁面優(yōu)化
1 修改配置文件
vim /opt/nginx112/conf/nginx.conf 在www.qishi2douyu.com虛擬主機下添加以下內(nèi)容(server代碼塊下) ? error_page 400 401 402 403 404 /40x.html;location = /40x.html {root /opt/qishi2douyu/;} ??
2 在/opt/qishi2douyu/目錄下創(chuàng)建40x.html, 把淘寶的錯誤頁面源代碼拷貝過來
vim 40x.html?
3 平滑重啟nginx
4 隨便訪問一個不存在的頁面
http://www.qishi2douyu.com/sladfj2435 就可以看到我們配置的錯誤頁面
?
5. nginx訪問日志功能
1 打開nginx配置文件nginx.conf
vim /opt/nginx112/conf/nginx.conf?
2 修改配置文件, 啟用日志功能
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;3 平滑重啟nginx
4 瀏覽器訪問192.168.12.56
5.查看訪問記錄
tail -f /opt/nginx112/logs/access.log6. nginx限制IP訪問
?
?
?
7. nginx代理功能
生活中的代理:
要想去租房:
租客 —> 中介 —>房東
代購, 海淘
?
正向代理
?
反向代理
?
nginx反向代理
1 準(zhǔn)備兩臺機器
192.168.12.56 ? # 內(nèi)部的django服務(wù)器192.168.12.77 ? # 代理服務(wù)器 請求數(shù)據(jù): windows ? ——> ? 192.168.12.77 ? ——> ? 192.168.12.56
返回數(shù)據(jù): windows ? <—— ? 192.168.12.77 ? <—— ? 192.168.12.56
?
2 修改代理服務(wù)器192.168.12.77的配置文件
vim /opt/nginx112/conf/nginx.conf
?
?
?
?
?
8. nginx負載均衡
?
nginx負載均衡配置
1 準(zhǔn)備三臺機器
1. nginx負載均衡器(192.168.12.77)2 另外兩臺應(yīng)用服務(wù)器(192.168.12.56 + 192.168.12.65)
?
2 先確保兩臺應(yīng)用服務(wù)器能夠正常訪問
http://192.168.12.56http://192.168.12.65:8001
3 配置負載均衡器(192.168.12.77)
修改配置文件 vim /opt/nginx112/conf/nginx.conf ? worker_processes 1; ? #error_log logs/error.log; 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;keepalive_timeout 65;#gzip on; ?upstream qishi2_xiaowei {server 192.168.12.56;server 192.168.12.65 weight=5;} ?server {listen 80;server_name www.qs.com;location / {root html;index index.html index.htm;proxy_pass http://qishi2_xiaowei;} ?#error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} }?
??
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/Mixtea/p/10724844.html
總結(jié)
以上是生活随笔為你收集整理的Linux之nginx入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell脚本[] [[]] -n -z
- 下一篇: Metro Win8风格的按钮(Filp