Nginx配置基于ip的虚拟主机
我是在centos7虚拟机上进行实验的
该实验基于添加好ip的基础上,如何给网卡添加ip请查阅我的博客
先来看一下我的ip
[root@localhost nginx]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.3.128 netmask 255.255.255.0 broadcast 192.168.3.255
inet6 fe80::6b14:b58e:faa2:525 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:5e:7c:27 txqueuelen 1000 (Ethernet)
RX packets 62179 bytes 43830505 (41.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 33693 bytes 4570753 (4.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.3.123 netmask 255.255.255.0 broadcast 192.168.3.255
ether 00:0c:29:5e:7c:27 txqueuelen 1000 (Ethernet)
ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.3.125 netmask 255.255.255.0 broadcast 192.168.3.255
ether 00:0c:29:5e:7c:27 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 185219 bytes 29342174 (27.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 185219 bytes 29342174 (27.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:61:e7:bc txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost nginx]#
安装好nginx之后(方法有说过了,这里不重复)
安装路径不知道你装哪里,直接 find / -name nginx.conf并进行编辑
[root@localhost nginx]# cat nginx.conf|grep -v "#"|grep -v "^$"
worker_processes 1;#这个参数一般可设为cpu核数,例如我的cpu是i3 3110m,那么这里我就可以通过修改为4来提高性能,但目前我还没进行到那步,所以就先在这里说一下而已
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80; #监听的端口,这里是默认的ip128
server_name localhost; #服务器名
location / {
root html; #网站目录,是nginx安装目录下的html
index index.html index.htm index.php; #默认页面
}
error_page 404 /404.html; #错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ { #相当于启用php吧
root html; #这里需要根据网站目录进行修改
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server{
listen 192.168.3.123:80;
server_name 192.168.3.123;
access_log logs/bp2.access.log combined; #日志路径,combined 日志格式名称
location /
{
index index.html index.php;
root html/bp2;
}
location ~ \.php$ {
root html/bp2;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server{
listen 192.168.3.125:80;
server_name 192.168.3.125;
location /{
root html/bp3;
index index.html index.php;
}
location ~ \.php$ {
root html/bp3;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
[root@localhost nginx]#
需要重启nginx而已,重启命令 nginx -s reload(这个命令需要进入到sbin文件夹下,不是那个地方都可以的)。
以后注意了,凡是修改了服务器的配置文件,一般都需要重启服务器
[root@localhost nginx]# cat html/bp3/index.php
<?php
echo bp3;
?>
[root@localhost nginx]#
来看一下效果