nginx動靜分離

搭建Nginx動靜分離
Nginx動靜分離介紹
Nginx的靜態處理能力很強,可是動態處理能力不足,所以,在企業中經常使用動靜分離技術
針對PHP的動靜分離php

靜態頁面交給Nginx處理
動態頁面交給PHP-FPM模塊或Apache處理
在Nginx的配置中,是經過location配置段配合正則匹配實現靜態與動態頁面的不一樣處理方式
反向代理原理
Nginx不只能做爲Web服務器,還具備反向代理、負載均衡和緩存的功能
Nginx經過proxy模塊實現將客戶端的請求代理至上游服務器,此時nginx與. 上游服務器的鏈接是經過http協議進行的
Nginx在實現反向代理功能時的最重要指令爲proxy_ pass, 它可以並可以根據URI、客戶端參數或其它的處理邏輯將用戶請求調度至上游服務器
配置動靜分離
架設並調試後端LAMP環境
安裝配置Nginx處理靜態頁面請求,在server {};段中加入
[root@nginx php5]#vim /usr/local/httpd/conf/nginx.conf
location ~ .*.(gifiglipeglbmp|swf)$ {
root html;
index index.html index.htm;
};
配置Nginx處理動態頁面請求,在server{};中加入
在Apache.工做目錄新建test.php
重啓Nginx並測試
[root@nginx php5]#vim /usr/local/httpd/conf/nginx.conf
server {
.....
location ~ .php$ {
proxy
pass http://192.168.9.237:8080; //LAMP的IP地址
......
配置實例
開啓兩臺Linux虛擬機,一臺配置Nginx,一臺配置LANM架構,客戶端訪問nginx網頁時Nginx服務處理靜態訪問信息,LAMP處理動態訪問信息,html

首先在一臺Linux系統中搭建lamp架構,這裏使用yum安裝lamp架構
yum install httpd httpd-devel -y
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
...//省略部份內容...
已安裝:
httpd.x86_64 0:2.4.6-90.el7.centos httpd-devel.x86_64 0:2.4.6-90.el7.centos
...//省略部份內容...
完畢!
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http
success //配置防火牆容許http服務在公共區域經過
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
success //配置防火牆容許https服務在公共區域經過
[root@localhost ~]# firewall-cmd --reload //從新加載防火牆
success
[root@localhost ~]# systemctl start httpd.service //啓動http服務
在客戶機訪問httpd服務,看服務是否成功開啓
nginx動靜分離mysql

安裝mariadb(mariadb是MySQL的分支軟件,功能與MySQL相同)
[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfilenginx

  • base: ftp.sjtu.edu.cn
    ...//省略部份內容...
    已安裝:
    mariadb.x86_64 1:5.5.64-1.el7 mariadb-devel.x86_64 1:5.5.64-1.el7
    mariadb-server.x86_64 1:5.5.64-1.el7
    完畢!
    [root@localhost ~]# systemctl start mariadb //啓動mariadb服務
    [root@localhost ~]# netstat -ntap | grep 3306
    tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2519/mysqld
    [root@localhost ~]# mysql_secure_installation //設置數據庫

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!sql

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.數據庫

Enter current password for root (enter for none): //詢問是否給root用戶設置密碼,直接回車
OK, successfully used password, moving on...vim

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.後端

Set root password? [Y/n] y //是否設置密碼,輸入y確認
New password: //輸入密碼
Re-enter new password: //再次輸入密碼
Password updated successfully!
Reloading privilege tables..
... Success!centos

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.緩存

Remove anonymous users? [Y/n] n //詢問是否刪除匿名用戶,輸入n,不刪除
... skipping.

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n //詢問是否拒絕root用戶遠程登陸,輸入n,不拒絕
... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n //詢問是否刪除測試數據庫,輸入n,不刪除
... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y //詢問是否從新加載數據庫,輸入y,從新加載
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB! //完成設置
安裝PHP
[root@localhost ~]# yum -y install php //安裝php服務
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile

  • base: ftp.sjtu.edu.cn
    ...//省略部份內容...
    已安裝:
    php.x86_64 0:5.4.16-46.1.el7_7
    ...//省略部份內容...
    完畢!
    [root@localhost ~]# yum install php-mysql -y //安裝php與mysql鏈接包
    已加載插件:fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
  • base: ftp.sjtu.edu.cn
  • extras: ftp.sjtu.edu.cn
    ...//省略部份內容...
    已安裝:
    php-mysql.x86_64 0:5.4.16-46.1.el7_7
    做爲依賴被安裝:
    php-pdo.x86_64 0:5.4.16-46.1.el7_7
    完畢!
    [root@localhost ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath //安裝php環境包
    已加載插件:fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
  • base: ftp.sjtu.edu.cn
    ...//省略部份內容...
    已安裝:
    libcurl-devel.x86_64 0:7.29.0-54.el7 php-bcmath.x86_64 0:5.4.16-46.1.el7_7
    php-gd.x86_64 0:5.4.16-46.1.el7_7 php-ldap.x86_64 0:5.4.16-46.1.el7_7
    php-mbstring.x86_64 0:5.4.16-46.1.el7_7 php-odbc.x86_64 0:5.4.16-46.1.el7_7
    php-pear.noarch 1:1.9.4-21.el7 php-snmp.x86_64 0:5.4.16-46.1.el7_7
    php-soap.x86_64 0:5.4.16-46.1.el7_7 php-xml.x86_64 0:5.4.16-46.1.el7_7
    php-xmlrpc.x86_64 0:5.4.16-46.1.el7_7
    ...//省略部份內容...
    完畢!
    [root@localhost ~]# cd /var/www/html //進入網頁站點
    [root@localhost html]# vim index.php //編輯php網頁內容
    <?php
    phpinfo();
    ?>
    :wq
    [root@localhost html]# systemctl restart httpd.service //重啓http服務
    在客戶機中測試LAMP機構是否安裝成功
    nginx動靜分離

安裝Nginx

相關文章
相關標籤/搜索