Nginx教程

nginx动静分离

本文主要是介绍nginx动静分离,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

nginx动静分离

httpd服务

[root@localhost src]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  debug  httpd-2.4.48.tar.gz  kernels
[root@localhost src]# tar xf httpd-2.4.48.tar.gz 
[root@localhost src]# tar xf apr-1.7.0.tar.gz 
[root@localhost src]# tar xf apr-util-1.6.1.tar.gz 
[root@localhost src]# cd apr-1.7.0
[root@localhost apr-1.7.0]# vim configure
cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"                注释或者删除此后


安装EPEL rpm 包
[root@localhost apr-1.7.0]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
[root@localhost apr-1.7.0]# ls /etc/yum.repos.d/
CentOS-Linux-AppStream.repo          CentOS-Linux-Plus.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-PowerTools.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Sources.repo
CentOS-Linux-Debuginfo.repo          epel-modular.repo
CentOS-Linux-Devel.repo              epel-playground.repo
CentOS-Linux-Extras.repo             epel-testing-modular.repo
CentOS-Linux-FastTrack.repo          epel-testing.repo
CentOS-Linux-HighAvailability.repo   epel.repo
CentOS-Linux-Media.repo
[root@localhost apr-1.7.0]# yum clean all
Failed to set locale, defaulting to C.UTF-8
21 files removed
[root@localhost apr-1.7.0]# yum makecache
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                           1.9 MB/s | 9.5 MB     00:04    
CentOS Linux 8 - BaseOS                              1.5 MB/s | 7.5 MB     00:05    
CentOS Linux 8 - Extras                              9.6 kB/s |  10 kB     00:01    
Extra Packages for Enterprise Linux Modular 8 - x86_ 333 kB/s | 955 kB     00:02    
Extra Packages for Enterprise Linux 8 - x86_64       1.6 MB/s |  11 MB     00:06    
Last metadata expiration check: 0:00:01 ago on Sun Oct 31 12:40:50 2021.
Metadata cache created.

安装开发工具包
[root@localhost apr-1.7.0]# yum groups mark install 'Development Tools' -y

创建用户
[root@localhost src]# useradd -r -M -s /sbin/nologin apache

安装依赖包
[root@localhost src]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make

编译安装
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make && make install
root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make && make install
[root@localhost httpd-2.4.48]# ./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr/ \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@localhost httpd-2.4.48]# make && make install

配置
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost ~]# source /etc/profile.d/httpd.sh
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man       添加
[root@localhost ~]# vim /usr/local/apache/conf/httpd.conf
ServerName www.example.com:80       取消注释

启动apache
[root@localhost ~]# apachectl start
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128                     *:80                    *:*                
LISTEN   0        128                  [::]:22                 [::]:*         

配置开机自启
[root@localhost ~]# vim /usr/lib/systemd/system/httpd.service
[root@localhost ~]# cat /usr/lib/systemd/system/httpd.service 
[Unit]
Description=Httpd server daemon
Documentation=man:httpd(8)
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# systemctl status httpd.service
● httpd.service - Httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: di>
   Active: inactive (dead) since Sun 2021-10-31 12:56:07 EDT; 6s ago
     Docs: man:httpd(8)
  Process: 85262 ExecStop=/usr/local/apache/bin/apachectl stop (code=exited, status=>
  Process: 85258 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, statu>

Oct 31 12:56:07 localhost.localdomain systemd[1]: Starting Httpd server daemon...
Oct 31 12:56:07 localhost.localdomain apachectl[85258]: httpd (pid 79944) already ru>
Oct 31 12:56:07 localhost.localdomain systemd[1]: httpd.service: Succeeded.
Oct 31 12:56:07 localhost.localdomain systemd[1]: Started Httpd server daemon.

启用代理模块
[root@localhost ~]# vim /usr/local/apache/conf/httpd.conf
#LoadModule remoteip_module modules/mod_remoteip.so
LoadModule proxy_module modules/mod_proxy.so                去掉注释
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so      去掉注释
#LoadModule proxy_scgi_module modules/mod_proxy_scgi.so

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

[root@lnmp ~]# nginx 
[root@lnmp ~]# systemctl start php-fpm.service
[root@lnmp ~]# systemctl start mysqld.service
[root@lnmp ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port         Peer Address:Port    
LISTEN    0          32              192.168.122.1:53                0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:22                0.0.0.0:*       
LISTEN    0          5                   127.0.0.1:631               0.0.0.0:*       
LISTEN    0          128                 127.0.0.1:9000              0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:111               0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:80                0.0.0.0:*       
LISTEN    0          128                      [::]:22                   [::]:*       
LISTEN    0          5                       [::1]:631                  [::]:*       
LISTEN    0          80                          *:3306                    *:*       
LISTEN    0          128                      [::]:111                  [::]:*      

[root@nginx ~]# nginx 
[root@nginx ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port         Peer Address:Port    
LISTEN    0          32              192.168.122.1:53                0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:22                0.0.0.0:*       
LISTEN    0          5                   127.0.0.1:631               0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:111               0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:80                0.0.0.0:*       
LISTEN    0          128                      [::]:22                   [::]:*       
LISTEN    0          5                       [::1]:631                  [::]:*       
LISTEN    0          128                      [::]:111                  [::]:*       

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip  on;
    upstream static {
        server 192.168.145.188;             http主机IP
    }

    upstream dynamic {
        server 192.168.145.170;             lnmp主机IP
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           # root   html;                   注释或删除
           # index  index.html index.htm;   注释或删除
           proxy_pass http://static;
        }

        #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://127.0.0.1;  取消注释
        }                                   取消注释
[root@nginx ~]# nginx -s reload
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful



这篇关于nginx动静分离的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!