nginx实现动静分离的方法示例

目录
  • 在server1上部署nginx
  • 在server上部署lnmp
  • node3部署httpd
  • 实现分离部署

本文主要介绍了nginx实现动静分离的方法示例,具有一定的学习价值,具体如下

环境:

系统/主机名 IP地址 服务
Redhat8 :server1 192.168.244.131 nginx
Redhat8:server2 192.168.244.133 lnmp
Content7:node3 192.168.244.142 httpd

在三台主机上关闭防火墙

[root@server1 ~]# systemctl stop firewalld
[root@server1 ~]# systemctl disable firewalld
[root@server1 ~]# vim /etc/selinux/config
SELINUX=disabled

在server1上部署nginx

[root@server1 opt]# cat nginx.sh
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "Please use administrator account"
        exit
fi

app_a=nginx-1.20.1.tar.gz

dir_a=/usr/local
dir_b=/var/log
dir_c=nginx-1.20.1

if [ ! -d $dir_b/nginx ];then
        mkdir -p $dir_b/nginx
fi
chown -R nginx.nginx $dir_b/nginx

yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++  make
yum -y groups mark install 'Development Tools'

id nginx &>/dev/null
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin nginx
fi

tar xf bag/$app_a -C $dir_a

cd  $dir_a/$dir_c
if [ ! -d $dir_a/nginx ];then
        ./configure \
                --prefix=$dir_a/nginx \
                --user=nginx \
                --group=nginx \
                --with-debug \
                --with-http_ssl_module \
                --with-http_realip_module \
                --with-http_image_filter_module \
                --with-http_gunzip_module \
                --with-http_gzip_static_module \
                --with-http_stub_status_module \
                --http-log-path=$dir_b/nginx/access.log \
                --error-log-path=$dir_b/nginx/error.log  && make  && make install
fi

cd ..
if [ ! -f /etc/profile.d/nginx.sh  ];then
        echo "export PATH=$dir_a/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
fi

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now nginx

查看端口

[root@server1 ~]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port                     Peer Address:Port
LISTEN   0        128                0.0.0.0:22                            0.0.0.0:*
LISTEN   0        128                0.0.0.0:80                            0.0.0.0:*
LISTEN   0        128                   [::]:22                               [::]:*

访问页面

在server上部署lnmp

部署nginx

[root@server2 lnmp]# cat install.sh
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "Please use administrator account"
        exit
fi

app_a=nginx-1.20.1.tar.gz

dir_a=/usr/local
dir_b=/var/log
dir_c=nginx-1.20.1

if [ ! -d $dir_b/nginx ];then
        mkdir -p $dir_b/nginx
fi
chown -R nginx.nginx $dir_b/nginx

yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++  make
yum -y groups mark install 'Development Tools'

id nginx &>/dev/null
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin nginx
fi

tar xf bag/$app_a -C $dir_a

cd  $dir_a/$dir_c
if [ ! -d $dir_a/nginx ];then
        ./configure \
                --prefix=$dir_a/nginx \
                --user=nginx \
                --group=nginx \
                --with-debug \
                --with-http_ssl_module \
                --with-http_realip_module \
                --with-http_image_filter_module \
                --with-http_gunzip_module \
                --with-http_gzip_static_module \
                --with-http_stub_status_module \
                --http-log-path=$dir_b/nginx/access.log \
                --error-log-path=$dir_b/nginx/error.log  && make  && make install
fi

cd ..
if [ ! -f /etc/profile.d/nginx.sh  ];then
        echo "export PATH=$dir_a/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
fi

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now nginx

部署mysql

[root@server2 lnmp]# cat mysql.sh
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "root?"
        exit
fi

dir_a=/usr/local
dir_b=/opt/data
app_a=mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
app_b=mysql-5.7.34-linux-glibc2.12-x86_64

id mysql  &>/dev/null
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin mysql
 fi

yum -y install ncurses-compat-libs ncurses-devel openssl-devel openssl cmake mariadb-devel 

if [ ! -d $dir_a/$app_b ];then
        tar xf bag/$app_a -C $dir_a
fi

if [ ! -d $dir_a/mysql ];then
        ln -sv $dir_a/$app_b  $dir_a/mysql
fi
chown -R mysql:mysql $dir_a/mysql*

echo "export PATH=$dir_a/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh

source /etc/profile.d/mysql.sh

if [ ! -d /$dir_b ];then
        mkdir -p /$dir_b
        chown -R mysql.mysql /$dir_b
fi

content=$(ls $dir_b | wc -l)
if [ $content -eq 0  ];then
        mysqld --initialize-insecure --user mysql --datadir $dir_b
fi

cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $dir_a/mysql
datadir = $dir_b
socket = /tmp/mysql.sock
port = 3306
pid-file = $dir_b/mysql.pid
user = mysql
skip-name-resolve
EOF

sed -ri "s#^(basedir=).*#\1$dir_a/mysql#g" $dir_a/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1$dir_b#g" $dir_a/mysql/support-files/mysql.server

cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.target 

[Service]
Type=forking

ExecStart=$dir_a/mysql/support-files/mysql.server start
ExecStop=$dir_a/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl  daemon-reload
systemctl  enable --now  mysqld

部署php

 https://www.php.net/distributions/php-8.0.10.tar.xz

解压

[root@server2 ~]# tar -xf  php-8.0.10.tar.gz  -C /usr/local/

安装依赖包

[root@server2 ~]# wget http://mirrors.aliyun.com/repo/epel-7.repo
[root@server1 ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel  php-mysqlnd  libsqlite3x-devel libzip-devel
[root@server2 ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

编译安装

[root@server2 ~]# cd /usr/local/php-8.0.10/
[root@server2 php-8.0.10]# ./configure --prefix=/usr/local/php8  --with-config-file-path=/etc --enable-fpm --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif  --enable-ftp --enable-gd --with-jpeg --with-zlib-dir --with-freetype --with-gettext --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix
......
......
......
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

[root@server2 php-8.0.10]# make
.......
.......
.......
invertedregexiterator.inc
pharcommand.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

[root@server2 php-8.0.10]# make install
......
......
/root/php-8.0.10/build/shtool install -c ext/phar/phar.phar /usr/local/php8/bin/phar.phar
ln -s -f phar.phar /usr/local/php8/bin/phar
Installing PDO headers:           /usr/local/php8/include/php/ext/pdo/

配置php-fpm

[root@server2 php-8.0.10]# cp /etc/php.ini /opt/
[root@server2 php-8.0.10]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@server2 php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@server2 php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@server2 php-8.0.10]# cd  ..
[root@server2 local]# cd  php8/
[root@server2 php8]# cd  etc/
[root@server2 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server2 etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@server2 etc]# cd  php-fpm.d
[root@server2 php-fpm.d]# ls
www.conf  www.conf.default

配置环境变量

[root@server2 ~]#  echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@server2 ~]# cat /etc/profile.d/php.sh
export PATH=/usr/local/php8/bin:$PATH
[root@server2 ~]# source /etc/profile.d/php.sh
[root@server2 ~]# which php
/usr/local/php8/bin/php

编写service文件

[root@server2 ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target 

[Service]
Type=forking

ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
[root@server2 ]# systemctl  daemon-reload

启动php

[root@server2 ~]# systemctl start php-fpm
[root@server2 ~]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port                     Peer Address:Port
LISTEN   0        128              127.0.0.1:9000                          0.0.0.0:*
LISTEN   0        128                0.0.0.0:80                            0.0.0.0:*
LISTEN   0        128                0.0.0.0:22                            0.0.0.0:*
LISTEN   0        80                       *:3306                                *:*
LISTEN   0        128                   [::]:22                               [::]:*  

在nginx.conf里配置虚拟主机

[root@server2 ~]# cd  /usr/local/nginx/html/
[root@server2 html]# ls
50x.html  index.html
[root@server2 html]# vim index.php
[root@server2 html]# cat index.php
<?php
    phpinfo();
?>
[root@server2 conf]# pwd
/usr/local/nginx/conf
[root@server2 conf]# vim  nginx.conf
........
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;

......
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
.....

        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;
        }
[root@server2 conf]# nginx -s reload

访问

node3部署httpd

[root@node3 ~]# yum -y install httpd

启动

[root@node3 ~]# systemctl start httpd
[root@node3 ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN     0      128     *:22                  *:*
LISTEN     0      100    127.0.0.1:25                  *:*
LISTEN     0      128    :::80                 :::*
LISTEN     0      128    :::22                 :::*
LISTEN     0      100       ::1:25                 :::*                 

访问

实现分离部署

在server1上的nginx.conf上配置

[root@server1 ~]# cat /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream cm {                #静态资源地址
        server 192.168.244.142;
    }

    upstream nm {                #动态资源地址
        server 192.168.244.133;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://cm;     #指向静态
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location ~ \.php$ {         #指向动态
            proxy_pass   http://nm;
        }

[root@server1 ~]# nginx -s reload

访问 192.168.244.131

在访问 192.168.244.131/index.php

到此这篇关于nginx实现动静分离的方法示例的文章就介绍到这了,更多相关nginx 动静分离内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Springmvc nginx实现动静分离过程详解

    在下自己整理 ,如有错误请指正 一般的nginx的 静态文件的项目是这么配置的 location ~ .*\.(js|css)?$ { root E:/Workspaces/Idea15/demo/web/WEB-INF; expires 1h; } 但是如果这样配置,系统是读取不到对应的文件的,因为springmvc本身的前端模板配置了访问静态资源 Handles 那如何使用nginx搭理访问,实现动静分离 搭建nginx代理 第一阶段,修改nginx.conf 文件 #location /

  • Nginx实现动静分离的示例代码

    结合本文场景,需要安装Nginx和Java环境(运行SpringBoot项目). 1.1 关于Linux系统安装Nginx可以参考我的文章---(传送门). 1.2 本文SpringBoot使用了Thymeleaf模板,项目端口号为8888. 1.3 在本地/Users/dalaoyang/Downloads/static文件内存放了jquery.js 2.什么是动静分离? 在弄清动静分离之前,我们要先明白什么是动,什么是静. 在Web开发中,通常来说,动态资源其实就是指那些后台资源,而静态资源

  • Nginx动静分离实现案例代码解析

    动静分离 通过中间件将动态请求和静态请求进行分离,减少不必要的请求消耗. 优势:动静服务互不影响,如动态服务停止,静态资源还可以正常访问. 实例 模拟环境:lb01.web01(模拟静态服务器).web02(模拟动态服务器) 1.在web01上创建目录/pm_code/images/,并上传一张图片 配置nginx文件/etc/nginx/conf.d/static.conf: 2.在web02上安装tomcat yum -y install tomcat systemctl start tom

  • nginx实现负载均衡和动静分离

    nginx配置(windows配置),供大家参考,具体内容如下 以下是我的项目用到的一份配置文件 #user nobody; worker_processes 4; #进程数,一般cpu是几核就写多少 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024;#单

  • nginx实现tomcat动静分离详解

    1.为什么要实现动静分离 1)nginx的处理静态资源能力超强 主要是nginx处理静态页面的效率远高于tomcat的处理能力,如果tomcat的请求量为1000次,则nginx的请求量为6000次,tomcat每秒的吞吐量为0.6M,nginx的每秒吞吐量为3.6M,可以说,nginx处理静态资源的能力是tomcat处理能力的6倍,优势可见一斑. 2)动态资源和静态资源分开,使服务器结构更清晰. 2.动静分离原理 服务端接收来自客户端的请求中,有一部分是静态资源的请求,例如html,css,j

  • nginx实现动静分离实例讲解

    为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度.降低原 来单个服务器的压力. 简单来说,就是使用正则表达式匹配过滤,然后交个不同的服务器. 1.准备环境 准备一个nginx代理 两个http 分别处理动态和静态. 1.配置编译安装的nginx为反向代理upstream: upstream static { server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s; } upstream php {

  • 部署Nginx+Apache动静分离的实例详解

    Nginx动静分离介绍 Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术 针对PHP的动静分离 静态页面交给Nginx处理 动态页面交给PHP-FPM模块或Apache处理 在Nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式 反向代理原理 Nginx不仅能作为Web服务器,还具有反向代理.负载均衡和缓存的功能 Nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时nginx与上游服务器的连接是通过http

  • Nginx负载均衡以及动静分离的原理与配置

    目录 一.Nginx实现负载均衡原理 二.Nginx动静分离原理 Nginx 静态处理优势 三.Nginx + Tomcat 动静分离.负载均衡配置步骤 环境准备: 动静分离配置 Nginx 负载均衡模式 总结 一.Nginx实现负载均衡原理 Nginx实现负载均衡是通过反向代理实现 Nginx服务器作为前端,Tomcat服务器作为后端,web页面请求由Nginx服务来进行转发. 但是不是把所有的web请求转发,而是将静态页面请求Nginx服务器自己来处理,动态页面请求,转发给后端的Tomcat

  • nginx实现动静分离的方法示例

    目录 在server1上部署nginx 在server上部署lnmp node3部署httpd 实现分离部署 本文主要介绍了nginx实现动静分离的方法示例,具有一定的学习价值,具体如下 环境: 系统/主机名 IP地址 服务 Redhat8 :server1 192.168.244.131 nginx Redhat8:server2 192.168.244.133 lnmp Content7:node3 192.168.244.142 httpd 在三台主机上关闭防火墙 [root@server

  • 解决Nginx端口冲突的排查方法示例

    问题描述 一个Spring + Angular前后端分离的项目,使用Nginx进行数据转发. Nginx监听端口8100,前台端口4200,后台端口8080. 像往常一样,提前配置好MySQL.配置好Redis,引入项目的Nginx配置文件,然后启动前台.后台,成功. 接下来出现了问题:前台发起的请求,只有极少数能被后台接收到,大部分都是404,随着在浏览器中的点击,控制台不断的出现404. 如果只是404,那问题就很简单,很大可能是Nginx端口转发设置错了.但它的神奇之处就在于,还有那么几次

  • 使用nginx实现动静分离

    本文为大家分享了使用nginx实现动静分离的具体内容,供大家参考,具体内容如下 一.什么是动静分离 动静分离是指在web服务器架构中,将静态页面与动态页面或者静态内容接口和动态内容接口分开不同系统访问的架构设计方法,进而提升整个服务访问性能和可维护性. nginx 的动静分离,指的是由 nginx 将客户端请求进行分类转发,静态资源请求(如html.css.图片等)由静态资源服务器处理,动态资源请求(如 jsp页面.servlet程序等)由 tomcat 服务器处理,tomcat 本身是用来处理

  • springboot结合mysql主从来实现读写分离的方法示例

    1.实现的功能 基于springboot框架,application.yml配置多个数据源,使用AOP以及AbstractRootingDataSource.ThreadLocal来实现多数据源切换,以实现读写分离.mysql的主从数据库需要进行设置数据之间的同步. 2.代码实现 application.properties中的配置 spring.datasource.druid.master.driver-class-name=com.mysql.jdbc.Driver spring.data

  • Apache结合Tomcat实现动静分离的方法

    实验环境 Apache和Tomcat均安装在IP地址为192.168.153.136的主机上 主机操作系统为centos7 实验之前关闭防火墙 ,命令: systemctl stop firewalld 关闭seliunx ,命令:setenforce 0 实验效果为Apache处理html静态资源,Tomcat处理jsp动态资源 1. Apache安装和启动 1.1 安装 yum -y install httpd 1.2 启动 systemctl start httpd 2. tomcat安装

  • Nginx location匹配规则的方法示例

    1.语法 location [=|~|~*|^~|@] /uri/ { ... } 2.说明 从上面的语法出发,可以了解到 location 可以区分为三个部分,接下来一个一个的研究一下. 1) [=|~|~*|^~|@] = : 表示精确匹配后面的url ~ : 表示正则匹配,但是区分大小写 ~* : 正则匹配,不区分大小写 ^~ : 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录 @ : "@" 定义一个命名的 location,使用在内部定向时,

随机推荐