ps: 所谓动静分离是把静态资源和动态资源分开,动态资源是要调取后端逻辑代码处理请求,而静态不需要调取逻辑代码处理请求。
环境:
192.168.136.144 nginx静态资源
192.168.136.129 nginx动态资源,反向代理
//nginx反向代理配置
upstream static {
server 192.168.136.144:80;
}
upstream php {
server 192.168.136.129:82;
}
server {
listen 80;
server_name localhost;
location ~ .*\.(html|jpg|png|css|js|jpeg)$ {
proxy_pass http://static;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
proxy_pass http://php;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
//静态资源模拟配置144
server {
listen 80;
server_name localhost;
location ~ \.(html|jpg|png|js|css|jpeg) {
root /home/www/html;
}
}
mkdir -p /home/www/html && cd /home && chown -R nginx.nginx ./www
ps: 随便上传一张图片至/home/www/html目录
//动态资源模拟配置129
server {
listen 82;
server_name localhost;
location ~ \.php$ {
root /home/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
mkdir -p /home/nginx/html && touch /home/nginx/html/index.php
file内容
<!DOCTYPE html>
<html>
<head>
<title>动态网站</title>
</head>
<body>
<?php
define('DB_USER', 'test-DT');
echo "<h1>".DB_USER."</h1>"
?>
<img src="/test.jpeg">
</body>
</html>
ps: 注意权限
访问:192.168.136.129/index.php就会返回如下内容