Nginx 動靜分離

.

一、作用描述

Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行;
其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好;
中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。

.
.
.

二、環境示意圖

.
.
Nginx 動靜分離
.
.
.
.

三、環境描述

.
. .三臺CentOS 7.5虛擬機
.
1、代理服務器:
服務:Nginx,並配置代理與動靜分離到後端兩臺動態與靜態服務器上
IP:192.168.27.134
.
.
2、靜態服務器:
服務:Nginx
IP:192.168.27.133
.
.
3、動態服務器:
服務:nginx、PHP,配置好動態頁面並讓nginx服務支持動態網頁,
IP:192.168.27.132
.
.
.
.

四、配置過程

.
1、三臺都要安裝Nginx
.
安裝epel源
[root@nginx ~]# yum install -y epel-release
安裝Nginx
[root@nginx ~]#yum install -y nginx
[root@nginx ~]#rpm -qa nginx
nginx-1.12.2-2.el7.x86_64
.
.
2、防火牆設置
三臺服務器都要放行80端口:
[root@nginx ~]#firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@nginx ~]#firewall-cmd --reload
.
.
3、靜態服務器
安裝lrzsz上傳靜態文件到服務器測試使用
[root@static ~]#yum install -y lrzsz
[root@static ~]#rz
找到想上傳到靜態服務器上的圖片
.
[root@static ~]#ls
5ab9d79a1ff95.jpg anaconda-ks.cfg
.
[root@static ~]#cp ./5ab9d79a1ff95.jpg /usr/share/nginx/html/
複製到web根目錄下
.
.
4、動態服務器
安裝PHP
[root@php ~]#yum install php php-fpm -y
.
配置Nginx支持PHP
[root@nginx ~]#rpm -ql nginx
查看文件位置
.
[root@php ~]#vi /etc/nginx/nginx.conf

......
server {
  listen       80;
  server_name  PHPserver;
  root         /usr/share/nginx/html;

  # Load configuration files for the default server block.
  include /etc/nginx/default.d/*.conf;
###<添加的
  location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
  }
###>
  location / {
  }
......

.
.
配置index動態主頁
[root@php ~]#vi /usr/share/nginx/html/index.php

<?php
phpinfo();
?>

.
啓動php-fpm
[root@php ~]#systemctl start php-fpm
[root@php ~]#ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 :
Php-fpm 默認監聽在9000端口
.
.
4、Nginx代理服務器
.
修改Nginx配置文件,動靜結合
[root@nginx ~]#vi /etc/nginx/nginx.conf

......
# for more information.
    include /etc/nginx/conf.d/*.conf;

###<添加的
    upstream static {
        server    192.168.27.133    max_fails=3 fail_timeout=10s;
        }       
    upstream dynamic {
        server    192.168.27.132    max_fails=3 fail_timeout=10s;
        }

###>

    server {
        listen       80;
        server_name  Nginx;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

###<添加的
        location ~\.(php|jsp)?$ {
                proxy_pass http://dynamic;
                              }
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
                proxy_pass http://static;
                                   }
###>

        location / {
        }
......

.
啓動Nginx
[root@nginx ~]#systemctl start nginx
.
.
.
.

五、測試結果

.
.
打開瀏覽器,輸入Nginx服務器的IP地址
.
.
Nginx 動靜分離
.
.
.
.
訪問先前上傳至靜態服務器上的圖片,還是Nginx服務器的IP
.
.
Nginx 動靜分離
.
.
.
.

訪問動態服務器上的PHP頁
.
.
Nginx 動靜分離
.
.
.
.
至此,測試算是完成,但還有許多配置這裏沒有用到,這只是一個最基礎的框架,還得努力完善

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章