search
HomeOperation and MaintenanceNginxHow to deploy Nginx+Apache and separate dynamic and static

nginx动静分离介绍

nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术
针对php的动静分离

  • 静态页面交给nginx处理

  • 动态页面交给php-fpm模块或apache处理

在nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式

反向代理原理

nginx不仅能作为web服务器,还具有反向代理、负载均衡和缓存的功能

nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时nginx与上游服务器的连接是通过http协议进行的

nginx在实现反向代理功能时的最重要指令为proxy_ pass,它能够并能够根据uri、客户端参数或其它的处理逻辑将用户请求调度至上游服务器

配置nginx实现动静分离

本案例根据企业需要,将配置nginx实现动静分离,对php页面的请求转发给lamp处理,而静态页面交给nginx处理,以实现动静分离

架构如图所示

How to deploy Nginx+Apache and separate dynamic and static

配置步骤

1、架设并调试后端lamp环境

①安装apache服务

[root@localhost ~]# yum install httpd httpd-devel -y

②在防火墙设置http服务的权限

[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
success   
[root@localhost ~]# firewall-cmd --reload 
success
[root@localhost ~]# systemctl start httpd

③安装mariadb

mariadb数据库管理系统是mysql的一个分支,主要由开源社区在维护,采用gpl授权许可 mariadb的目的是完全兼容mysql,包括api和命令行,使之能轻松成为mysql的代替品

[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
[root@localhost ~]# systemctl start mariadb.service

④mysql安全配置向导

[root@localhost ~]# mysql_secure_installation

⑤安装php及支持的软件

[root@localhost ~]# yum install php -y
[root@localhost ~]# yum install php-mysql -y
[root@localhost ~]# yum install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath -y

⑥更改网页主页面

[root@localhost ~]# cd /var/www/html
[root@localhost html]# vim index.php
<?php
  echo "this is apache test web";
?>

[root@localhost html]# systemctl restart httpd

⑦访问测试,输入网址

How to deploy Nginx+Apache and separate dynamic and static

2、编译安装nginx

①安装支持软件

[root@localhost ~]# yum install gcc gcc-c++ pcre-devel zlib-devel -y

②创建运行用户和组

[root@localhost ~]# useradd -m -s /sbin/nologin nginx

③编译安装

[root@localhost lnmp-c7]# tar zxvf nginx-1.12.2.tar.gz -c /opt
[root@localhost lnmp-c7]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

④服务管理控制

[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: ngins service control script
prog="/usr/local/nginx/sbin/nginx"
pidf="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
  $prog
  ;;
stop)
  kill -s quit $(cat $pidf)
  ;;
restart)
   $0 stop
   $0 start
   ;;
reload)
   kill -s hup $(cat $pidf)
   ;;
*)
   echo "usage: $0 {start|stop|restart|reload}"
   exit 1
esac
exit 0

[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# service nginx start

⑤启动服务

[root@nginx ~]# systemctl stop firewalld.service
[root@nginx ~]# setenforce 0
[root@nginx ~]# service nginx start

⑥配置nginx处理动态页面请求

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    location ~ \.php$ {
      proxy_pass  http://192.168.150.214;
    }

[root@nginx ~]# service nginx restart

⑦访问测试

How to deploy Nginx+Apache and separate dynamic and static
How to deploy Nginx+Apache and separate dynamic and static

The above is the detailed content of How to deploy Nginx+Apache and separate dynamic and static. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure nginx in WindowsHow to configure nginx in WindowsApr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve nginx403 errorHow to solve nginx403 errorApr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to start nginx in LinuxHow to start nginx in LinuxApr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check whether nginx is started?How to check whether nginx is started?Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to solve nginx304 errorHow to solve nginx304 errorApr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment