配置LANMP环境-- 配置nginx反向代理,与配置apache虚拟主机
Posted shifu204
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置LANMP环境-- 配置nginx反向代理,与配置apache虚拟主机相关的知识,希望对你有一定的参考价值。
一、配置nginx反向代理
1.修改配置文件
vim /etc/nginx/nginx.conf
在35行http下添加一下内容: include /data/nginx/vhosts/*.conf; include /etc/nginx/upstream.conf;
2.在/etc/nginx/目录下新建 upstream.conf文件
vim upstream.conf
upstream dev.test1.com { server 127.0.0.1(换成虚拟机ip):8080 weight=4 max_fails=2 fail_timeout=10s; ip_hash; } upstream dev,test2.com { server 127.0.0.1(换成虚拟机ip):8080 weight=4 max_fails=2 fail_timeout=10s; ip_hash; } upstream dev.test3.com { server 127.0.0.1(换成虚拟机ip):8080 weight=4 max_fails=2 fail_timeout=10s; ip_hash; }
3.在/etc/nginx/目录下新建 proxy.conf
vim proxy.conf
写入以下内容: proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;
4.在/data/nginx/vhosts/目录下新建test.conf配置文件
mkdir -p /data/nginx/vhosts
mkdir -p /data/nginx/logs
cd /data/nginx/vhosts
vim test.conf
写入以下内容:
server {
listen 80;
server_name dev.test1.com;
charset UTF-8;
access_log /data/nginx/logs/test1.access.log combined;
location /{
proxy_pass http://dev.test1.com;
#Header
include "/etc/nginx/proxy.conf";
}
error_page 404 /error.html;
location = /error.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name dev.test2.com;
charset UTF-8;
access_log /data/nginx/logs/test2.access.log combined;
location /{
proxy_pass http://dev.test2.com;
#Header
include "/etc/nginx/proxy.conf";
}
error_page 404 /error.html;
location = /error.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name dev.test3.com;
charset UTF-8;
access_log /data/nginx/logs/test3.access.log combined;
location /{
proxy_pass http://dev.test3.com;
#Header
include "/etc/nginx/proxy.conf";
}
error_page 404 /error.html;
location = /error.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
二、配置apache虚拟主机
1.载入虚拟主机配置文件
vim /etc/httpd/conf/httpd.conf
在文件最低下添加以下内容:
include conf/extra/httpd-vhosts.conf
2.在/etc/httpd/conf/ 目录下新建extra目录,并在extra目录下新建httpd-vhosts.conf配置文件
cd /etc/httpd/conf/ mkdir extra cd /etc/httpd/conf/extra vim httpd-vhosts.conf
添加以下内容:
<VirtualHost 127.0.0.1(换成虚拟机ip):8080>
ServerAdmin "[email protected]"
DocumentRoot "/data/www/test1"
ServerName dev.test1.com
ErrorLog "/data/httpd/logs/test1.net-error.log"
CustomLog "/data/httpd/logs/test1.net-access.log" common
<Directory "/">
Options FollowSymLinks
AllowOverride All
Order Deny,Allow
Require all granted
</Directory>
<Directory ".git">
Deny from All
Require all denied
</Directory>
</VirtualHost>
<VirtualHost 127.0.0.1(换成虚拟机ip):8080>
ServerAdmin "[email protected]"
DocumentRoot "/data/www/test2"
ServerName dev.test2.com
ErrorLog "/data/httpd/logs/test2.net-error.log"
CustomLog "/data/httpd/logs/test2.net-access.log" common
<Directory "/">
Options FollowSymLinks
AllowOverride All
Order Deny,Allow
Require all granted
</Directory>
<Directory ".git">
Deny from All
Require all denied
</Directory>
</VirtualHost>
<VirtualHost 127.0.0.1(换成虚拟机ip):8080>
ServerAdmin "[email protected]"
DocumentRoot "/data/www/test3"
ServerName dev.test3.com
ErrorLog "/data/httpd/logs/test3.net-error.log"
CustomLog "/data/httpd/logs/test3.net-access.log" common
<Directory "/">
Options FollowSymLinks
AllowOverride All
Order Deny,Allow
Require all granted
</Directory>
<Directory ".git">
Deny from All
Require all denied
</Directory>
</VirtualHost>
2.在/data/httpd/下面创建logs文件夹
cd /data/httpd/
mkdir logs
3.配置window的ip解析,打开C:\Windows\System32\drivers\etc下面的hosts文件添加下面三行。
192.168.116.128(换成虚拟机ip) dev.test1.com 192.168.116.128(换成虚拟机ip) dev.test2.com
192.168.116.128(换成虚拟机ip) dev.test3.com
4.添加项目目录与测试文件
mkdir -p /data/www/test1
mkdir -p /data/www/test2
mkdir -p /data/www/test3
cd /data/www/test1
vim index.php
输入以下虚线前的内容:
<?php
echo "test1";
-------------------------------------------------
cd /data/www/test2
vim index.php
输入以下虚线前的内容:
<?php
echo "test2";
-------------------------------------------------
cd /data/www/test3
vim index.php
输入以下内容:
<?php
echo "test3";
三、检查配置成功与否
全部配置好之后重启nginx和apache,如果重启失败可以用命令status nginx【httpd】,或者journalctl -xe查看那里出现问题。
常见启动失败的原因:
1. logs目录或者其他关键目录没有创建。
2. 防火墙禁用了8080端口
如果一切顺利,全部配置成功则:在浏览器访问dev.test1.com会出现对应/data/www/test1/index.php文件内容的页面。
以上是关于配置LANMP环境-- 配置nginx反向代理,与配置apache虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章