linux基礎之nginx 詳述03
阿新 • • 發佈:2022-01-06
目錄
nginx
1、location
使用Nginx Location可以控制訪問網站的路徑, 但一個server可以有多個location配置, 多個location的具有不同的優先順序區分。
1.1 location匹配符號
匹配符 | 匹配規則 | 優先順序 |
---|---|---|
= | 精確匹配 | 1 |
^~ | 以某個字串開頭 | 2 |
~ | 區分大小寫的正則匹配 | 3 |
~* | 不區分大小寫的正則匹配 | 3 |
/ | 通用匹配,任何請求都會匹配到 | 4 |
# 優先順序測試:測出一個優先順序就註釋掉一個location,再重啟nginx繼續測試下一個優先順序。 1.編輯配置檔案 [root@web03 conf.d]# vim game4.conf server { listen 80; server_name _; location ~* /python { default_type text/html; return 200 "Location ~*"; } location ~ /Python { default_type text/html; return 200 "Location ~"; } location ^~ /python { default_type text/html; return 200 "Location ^~"; } location = /python { default_type text/html; return 200 "Location ="; } } 2.重啟nginx,然後去瀏覽器訪問http://192.168.15.9/python [root@web03 conf.d]# systemctl restart nginx
1.2 案例:把超級瑪麗的圖片資料夾共享到NFS
1.建立掛載點並給許可權 root@nfs ~]# cd /opt [root@nfs opt]# mkdir /opt/img [root@nfs opt]# ll [root@nfs opt]# vim /etc/exports # 新增以下內容後儲存退出 /opt/img 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666) [root@nfs opt]# chown -R www.www /opt/img/ [root@nfs opt]# cd 2.重啟nfs和rpcbind [root@nfs ~]# systemctl restart nfs-server rpcbind 3.檢視當前掛載點是否有剛剛建立的點 [root@nfs ~]# showmount -e 4.去客戶端掛載 [root@web03 opt]# mkdir image [root@web03 opt]# cd image [root@web03 image]# mkdir images [root@web03 opt]# mount -t nfs 172.16.1.31:/opt/img /opt/image 5.檢視掛載 [root@web03 opt]# df -h 6.移動圖片資料夾的所有檔案到掛載的目錄 [root@web03 opt]# mv Super_Marie/images/* /opt/image/images/ 7.刪除原圖片資料夾 [root@web03 Super_Marie]# rm -rf images/ 8.修改配置檔案,鍵入以下內容並儲存 [root@web03 conf.d]# vim game5.conf server { listen 80; server_name 192.168.15.9; location / { root /opt/Super_Marie; index index.html; } } 9.重啟nginx,然後去瀏覽器訪問192.168.15.9,頁面無圖片。 [root@web03 conf.d]# systemctl restart nginx 10.修改配置檔案,新增一個location來新增圖片訪問的路徑 [root@web03 conf.d]# vim game5.conf server { listen 80; server_name 192.168.15.9; location / { root /opt/Super_Marie; index index.html; } location ~ /image { root /opt/image; } } 11.重啟nginx,然後去瀏覽器訪問192.168.15.9,頁面有圖片了。 [root@web03 conf.d]# systemctl restart nginx 12.在其他的客戶端也新增掛載點然後掛載到NFS就可以實現共享了。
2、LNMP架構
LNMP是一套技術的組合,L=Linux、N=Nginx、M~=MySQL、P~=Python
首先Nginx服務是不能處理動態請求,那麼當用戶發起動態請求時, Nginx又是如何進行處理的。
1.靜態請求:請求的內容是靜態檔案就是靜態請求
1)靜態檔案:檔案上傳到伺服器,永遠不會改變的檔案就是靜態檔案
2)html就是一個標準的靜態檔案
2.動態請求:請求的內容是動態的就是動態請求
1)不是真實存在伺服器上的內容,是通過資料庫或者其他服務拼湊成的資料
當用戶發起http請求,請求會被Nginx處理,如果是靜態資源請求Nginx則直接返回,如果是動態請求Nginx則通過uwsgi協議轉交給後端的Python程式處理
2.1 uwsgi
因為nginx不支援wsgi協議,無法直接呼叫py開發的webApp(python開發的網站)。
在nginx + uwsgi + Django的框架裡,nginx代理+ webServer,uwsgi是wsgiServer,Django是webApp。
nginx接收使用者請求,並判定哪些轉發到uWsgi,uWsgi再去呼叫pyWebApp。
2.2 uwsgi服務部署
1、建立使用者:統一使用者
[root@web03 opt]# groupadd django -g 888
[root@web03 opt]# useradd django -u 888 -g 888 -r -M -s /bin/sh
2、安裝依賴軟體
[root@web03 opt]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
3、安裝Django和uwsgi
[root@web03 opt]# pip3 install django
[root@web03 opt]# pip3 install uwsgi
4、建立專案
[root@web03 opt]# cd /opt
[root@web03 opt]# django-admin startproject linux
[root@web03 opt]# cd linux
[root@web03 opt]# django-admin startapp app01
[root@web03 linux]# vim linux/settings.py
ALLOWED_HOSTS = ['*'] # 允許所有的ip都能訪問
DATABASES = {}
# 啟動測試,然後去瀏覽器訪問192.168.15.9:8000
[root@web03 linux]# python3 manage.py runserver 0.0.0.0:8000
5、編輯專案配置檔案:鍵入以下內容並儲存
[root@localhost ~]# vim /opt/linux/myweb_uwsgi.ini
[uwsgi]
# 埠號
socket = :8000
# 指定專案的目錄
chdir = /opt/linux
# wsgi檔案路徑
wsgi-file = linux/wsgi.py
# 模組wsgi路徑
module = linux.wsgi
# 是否開啟master程序
master = true
# 工作程序的最大數目
processes = 4
# 結束後是否清理檔案
vacuum = true
6、啟動uwsgi
[root@web03 linux]# uwsgi -d --ini myweb_uwsgi.ini --uid 666
-d : 以守護程序方式執行
--ini : 指定配置檔案路徑
--uid : 指定uid
# 這時候再去瀏覽器訪問192.168.15.9:8000已經不能訪問了, 因為uwsgi是一種TCP服務,需要轉換成http服務才能在瀏覽器訪問。TCP 服務包含http,http不包含tcp。如果需要訪問的話就需要nginx來進行轉換。
7、編輯Nginx配置檔案:鍵入以下內容並儲存
[root@web03 nginx]# cd /etc/nginx/conf.d/
[root@web03 conf.d]# ll
[root@web03 conf.d]# vim /etc/nginx/conf.d/python.conf
server {
listen 80;
server_name py.test.com;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
uwsgi_param UWSGI_SCRIPT linux.wsgi;
uwsgi_param UWSGI_CHDIR /opt/linux;
index index.html index.htm;
client_max_body_size 35m;
}
}
8、重啟Nginx配置
[root@web03 conf.d]# systemctl restart nginx
9、域名解析完成後去瀏覽器訪問 172.16.1.9即可訪問成功。
C:\Windows\System32\drivers\etc
172.16.1.9 py.test.com
# 每個語言都有對應配置的params。
[root@web03 linux]# cd /etc/nginx
[root@web03 nginx]# ll
uwsgi_params : 給python用的
scgi_params : 給java用的
fastcgi_params : 給PHP用的
V2_params : 給Go用的,目前linux不支援
10、壓力測試:
[root@web03 conf.d]# cd /opt/linux
[root@web03 linux]# python3 manage.py runserver 0.0.0.0:8001
# 在瀏覽器訪問192.168.15.9:8001
[root@web03 linux]# cd /etc/nginx/conf.d
[root@web03 conf.d]# vim py.conf
server {
listen 80;
server_name py1.test.com;
location / {
proxy_pass 127.0.0.1:8001;
}
}
# 域名解析
C:\Windows\System32\drivers\etc
172.16.1.9 py1.test.com
# 換一個客戶端進行域名解析再進行壓測
[root@web02 ~]# cd /opt
[root@web02 opt]# vim /etc/hosts
172.16.1.9 py.test.com py1.test.com
[root@web02 opt]# curl -I -H'Host: py1.test.com' 172.16.1.9
[root@web02 opt]# curl -I -H'Host: py.test.com' 172.16.1.9
# netstat -nutlp # 檢視埠使用狀態
3、部署BBS專案
1、部署資料庫
[root@db01 ~]# yum install mariadb* -y
2、啟動資料庫
[root@db01 ~]# systemctl start mariadb
3、遠端連線MySQL資料:在資料庫輸入以下命令
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
CREATE DATABASE `bbs` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
# 然後去pycharm新增連線SQL,name輸入db01,Host輸入172.16.1.61,User輸入root,Password輸入123456,點test connection測試通過後點OK即可完成遠端連線。
4、部署BBS
4.1、上傳程式碼然後解壓,更改路徑
[root@db01 ~]# unzip bbs.zip
[root@db01 ~]# mv bbs /opt/
4.2、資料庫遷移(資料庫遷移前:只留__init__,,其他都刪掉)
[root@web03 migrations]# cd /opt/bbs/app01/migrations/
[root@web03 migrations]# pwd
/opt/bbs/app01/migrations
[root@web03 migrations]# rm -rf 00*
[root@web03 migrations]# rm -rf __pycache__/
[root@web03 migrations]# ll
[root@web03 migrations]# cd /opt/bbs/
[root@web03 bbs]# pwd
/opt/bbs
# 修改Django版本
[root@web03 bbs]# pip3 uninstall django
[root@web03 bbs]# pip3 install django==1.11
# 安裝MySQL資料庫外掛
[root@web03 bbs]# pip3 install pymysql
# 修改資料連線修改以下內容並儲存:
[root@web03 bbs]# vim bbs/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bbs',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '172.16.1.61',
'PORT': 3306,
'CHARSET': 'utf8'
}
}
# 建立資料庫遷移檔案
[root@web03 bbs]# python3 manage.py makemigration
# 資料庫遷移
[root@web03 bbs]# python3 manage.py migrate
4.3、配置UWSGI(wsgi是C語言寫的,啟動了之後直接繞過python,提高效能。)
[root@localhost ~]# vim /opt/bbs/myweb_uwsgi.ini
[uwsgi]
# 埠號
socket = :8002
# 指定專案的目錄
chdir = /opt/bbs
# wsgi檔案路徑
wsgi-file = bbs/wsgi.py
# 模組wsgi路徑
module = bbs.wsgi
# 是否開啟master程序
master = true
# 工作程序的最大數目
processes = 4
# 結束後是否清理檔案
vacuum = true
[root@web03 bbs]# uwsgi --ini myweb_uwsgi.ini --uid 666
[root@web03 bbs]# ps -ef | grep uwsgi
[root@web03 bbs]# kill -9 3260
[root@web03 bbs]# kill -9 13708
4.4、配置Nginx並重啟nginx
[root@localhost ~]# vim /etc/nginx/conf.d/bbs.conf
server {
listen 80;
server_name bbs.test.com;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8002;
uwsgi_read_timeout 2;
uwsgi_param UWSGI_SCRIPT bbs.wsgi;
uwsgi_param UWSGI_CHDIR /opt/bbs;
index index.html index.htm;
client_max_body_size 35m;
}
}
[root@web03 bbs]# systemctl restart nginx
4.5、域名解析後瀏覽器訪問 bbs.test.com
C:\Windows\System32\drivers\etc
172.16.1.9 bbs.test.com