1. 程式人生 > 其它 >linux中LNMP架構和location用法

linux中LNMP架構和location用法

目錄

location

使用Nginx Location可以控制訪問網站的路徑,但一個server可以有多個location配置, 多個location的優先順序該如何區分

location匹配符號

匹配符 匹配規則 優先順序
= 精確匹配 1
^~ 以某個字串開頭 2
~ 區分大小寫的正則匹配 3
~* 不區分大小寫的正則匹配 3
/ 通用匹配,任何請求都會匹配到 4
[root@web02 conf.d]# vim supermary.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 =";
    }
}

網站訪問game001.com/python,每次訪問之後修改配置檔案,會產生一下效果.

案例

我們把超級瑪麗裡的images裡的圖片放到共享檔案下/opt/imgaes然後刪除超級瑪麗裡的images,然後這時會遊戲會顯示黑屏,因為找不到圖片

我們檢視下錯誤日誌

然後我們再做如下操作

server {
   listen 80;
   server_name game001.com;
   location / {
        root /opt/Super_Mary;
        index index.html;
   }
   location ^~ /images {   # 新寫一個location 以image開頭  在/opt目錄下查詢
        root /opt;
   }
}

再次訪問超級瑪麗遊戲,就又可以遊玩啦!!!

LNMP架構

1.簡介

LNMP是一套技術的組合,L=Linux、N=Nginx、M=MySQL、P=PYTHON或PHP
不僅僅只有這些服務,還有很多

首先Nginx服務是不能處理動態請求,那麼當用戶發起動態請求時, Nginx又是如何進行處理的。
1.靜態請求:請求的內容是靜態檔案就是靜態請求
1)靜態檔案:檔案上傳到伺服器,永遠不會改變的檔案就是靜態檔案
2)html就是一個標準的靜態檔案
2.動態請求:請求的內容是動態的就是動態請求
1)不是真實存在伺服器上的內容,是通過資料庫或者其他服務拼湊成的資料

當用戶發起http請求,請求會被Nginx處理,如果是靜態資源請求Nginx則直接返回,如果是動態請求Nginx則通過uwsgi協議轉交給後端的Python程式處理

2.uwsgi

  • 因為nginx不支援wsgi協議,無法直接呼叫py開發的webApp。
  • 在nginx+uWsgi+Django的框架裡,nginx代理+webServer,uWsgi是wsgiServer,Django是webApp。
  • nginx接收使用者請求,並判定哪些轉發到uWsgi,uWsgi再去呼叫pyWebApp。

3.uwsgi服務部署

# 檢視埠
netstat -ntlp
kill +埠  殺死程序
tail -f /var/log/nginx/access.log
1、建立使用者
[root@web01 opt]# groupadd django -g 888
[root@web01 opt]# useradd django -u 888 -g 888 -r -M -s /bin/sh

2、安裝依賴軟體
[root@web01 opt]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y

3、安裝Django和uwsgi
[root@web01 opt]# pip3 install django==1.11
[root@web01 opt]# pip3 install uwsgi

4、建立專案
[root@web01 opt]# cd /opt
[root@web01 opt]# django-admin startproject linux
[root@web01 opt]# cd linux
[root@web01 opt]# django-admin startapp app01
[root@web01 linux]# vim linux/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {}
# 啟動測試
[root@web01 linux]# python3 manage.py runserver 0.0.0.0:8000

不同版本的django顯示的圖片不一樣.

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@web01 linux]# uwsgi -d --ini myweb_uwsgi.ini --uid 666

-d 	  : 以守護程序方式執行
--ini : 指定配置檔案路徑
--uid : 指定uid

7、編輯Nginx配置檔案
[root@localhost ~]# 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配置
systemctl restart nginx
在window裡的hosts檔案新增172.16.1.8  py.test.com
訪問 py.test.com

4.nginx代理python服務(壓力測試)

[root@web01 linux]# python3 manage.py runserver 0.0.0.0:8001
[root@web02 conf.d]# vim py1.conf
server {
    listen 80;
    server_name py1.test.com;
    location / {
        proxy_pass http://127.0.0.1:8001;
    }
}

[root@web02 conf.d]# systemctl restart nginx

[root@web02 conf.d]# vim /etc/hosts
172.16.1.8  py.test.com  py1.test.com
[root@web02 conf.d]# curl -I -H'Host: py.test.com' 172.16.1.8
[root@web02 conf.d]# curl -I -H'Host: py1.test.com' 172.16.1.8

# 開始壓力測試
[root@web02 conf.d]# ab -n 10000 -c 10 http://py.test.com/
# 這是uwsgi代理  Time taken for tests:   6.958 seconds
[root@web02 conf.d]# ab -n 10000 -c 10 http://py1.test.com/
# 這是python代理  Time taken for tests:   20.714 seconds

由此可以看出uwsgi代理要比python要快很多.

部署BBS專案

1、部署資料庫
[root@db01 ~]# yum install mariadb* -y

2、啟動資料庫
[root@db01 ~]# systemctl start mariadb
# 執行資料庫
[root@db01 ~]# mysql

3、遠端連線MySQL資料
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;FLUSH PRIVILEGES;
# 這裡的使用者名稱和密碼根據自己設定

4.建立BBS資料庫
CREATE DATABASE `bbs` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

開啟pycharm

4.1、上傳程式碼
[root@web01 ~]# unzip bbs.zip
[root@web01 ~]# mv bbs /opt/

4.2、資料庫遷移
[root@web01 migrations]# cd /opt/bbs/app01/migrations
[root@web01 migrations]# rm -rf 00*
[root@web01 migrations]# rm -rf __pycache__/

[root@web01 migrations]# cd /opt/bbs/

# 安裝MySQL資料庫外掛
[root@web01 bbs]# pip3 install pymysql

# 修改資料連線
[root@web01 bbs]# vim bbs/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'USER': 'root',
        'PASSWORD': '123',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}

# 建立資料庫遷移檔案
[root@web01 bbs]# python3 manage.py makemigrations

# 資料庫遷移
[root@web01 bbs]# python3 manage.py migrate

4.3、配置UWSGI
[root@web01 bbs]# 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

# 測試UWSGI配置是否成功
[root@web01 bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666

4.4、配置Nginx
[root@localhost ~]# vim /etc/nginx/conf.d/python.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@web01 bbs]# systemctl restart nginx

把bbs.test.com加入window裡的hosts配置檔案裡

開啟bbs.test.com