- 1.配置动静分离
- 1.1 环境准备
- 1.2 安装服务
- 1.3 修改nginx主机配置文件
- 1.4 测试
| 主机名 | IP | 服务 |
|---|---|---|
| nginx | 192.168.237.170 | nginx |
| lnmp | 192.168.237.167 | lnmp架构 |
| httpd | 192.168.237.168 | httpd |
安装nginx
//编译安装nginx [root@nginx scripts]# ./nginx.sh [root@nginx scripts]# nginx [root@nginx ~]# ss -anltu Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:* tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* tcp LISTEN 0 128 [::]:22 [::]:*
访问
搭建lnmp
[root@lnmp scripts]# ./lnmp.sh [root@lnmp scripts]# ss -anltu Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 128 127.0.0.1:9000 0.0.0.0:* tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* tcp LISTEN 0 80 *:3306 *:* tcp LISTEN 0 128 *:80 *:* tcp LISTEN 0 128 [::]:22 [::]:*
访问
安装apache
//编译安装apache [root@httpd scripts]# ./apache.sh [root@httpd scripts]# ss -anltu Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* tcp LISTEN 0 128 [::]:22 [::]:* tcp LISTEN 0 128 *:80 *:*
访问
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
33 #gzip on;
##添加以下6行
34 upstream static{
35 server 192.168.237.168; //httpd主机的IP
36 }
37
38 upstream dynamic{
39 server 192.168.237.167; //lnmp主机的IP
40 }
41
42 server {
43 listen 80;
44 server_name localhost;
45
46 #charset koi8-r;
47
48 #access_log logs/host.access.log main;
49
50 location / {
51 #root html; //注释或删掉
52 #index index.html index.htm; //注释或删掉
53 proxy_pass http://static; //访问静态资源会自动跳转到进行访问
54 }
55
56 #error_page 404 /404.html;
57
58 # redirect server error pages to the static page /50x.html
59 #
60 error_page 500 502 503 504 /50x.html;
61 location = /50x.html {
62 root html;
63 }
64
65 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
66 #
## 取消以下三行注释,并修改
67 location ~ .php$ {
68 proxy_pass http://dynamic; //访问动态资源会自动跳转到进行访问
69 }
[root@localhost conf]# 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
[root@localhost conf]# nginx -s reload
[root@localhost conf]# ss -anltu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*
1.4 测试
使用nginx主机IP访问测试
访问静态资源
访问动态资源



