Nginx 虚拟主机配置

Nginx 虚拟主机配置

1、主配置文件下加入虚拟主机参数。
vim nginx.conf

include vhosts/*.conf;
配置文件

2、创建虚拟主机目录,文件。

mkdir /usr/local/nginx/conf/vhosts
创建虚拟主机目录
touch /usr/local/nginx/conf/vhosts/nginx_vhosts1.conf
touch /usr/local/nginx/conf/vhosts/nginx_vhosts2.conf
touch /usr/local/nginx/conf/vhosts/nginx_vhosts3.conf
创建虚拟主机文件

基于IP

1、添加IP地址。

ifconfig eth0:1 192.168.1.131
ifconfig eth0:2 192.168.1.132
ifconfig eth0:3 192.168.1.133

2、修改虚拟主机文件。

# 基于IP 主机1
server{
   # 监听端口
   listen 192.168.1.131:80;
   # 访问域名
   server_name www.xxx.com;
   # 网页索引类型
   index index.html index.htm;
   # 指定URL资源
   root /usr/local/url1;
}
修改虚拟主机1
# 基于IP 主机2
server{
   # 监听端口
   listen 192.168.1.132:80;
   # 访问域名
   server_name www.xxx.com;
   # 网页索引类型
   index index.html index.htm index.php;
   # 指定URL资源
   root /usr/local/url2;
}
修改虚拟主机2
# 基于IP 主机3
server{
   # 监听端口
   listen 192.168.1.133:80;
   # 访问域名
   server_name www.xxx.com;
   # 网页索引类型
   index index.html index.htm index.php;
   # 指定URL资源
   root /usr/local/url3;
}
修改虚拟主机3

基于域名

1、修改hosts文件,或使用DNS解析。

C:WindowsSystem32driversetc

192.168.1.115 www.131.com www.132.com www.133.com
修改hosts文件

 2、修改虚拟主机文件。

# 基于域名 主机1
server{
   # 监听端口
   listen 80;
   # 访问域名
   server_name www.131.com;
   # 网页索引类型
   index index.html index.htm;
   # 指定URL资源
   root /usr/local/url1;
}
修改虚拟主机1
# 基于域名 主机2
server{
   # 监听端口
   listen 80;
   # 访问域名
   server_name www.132.com;
   # 网页索引类型
   index index.html index.htm;
   # 指定URL资源
   root /usr/local/url2;
}
修改虚拟主机2
# 基于域名  主机3
server{
   # 监听端口
   listen 80;
   # 访问域名
   server_name www.133.com;
   # 网页索引类型
   index index.html index.htm;
   # 指定URL资源
   root /usr/local/url3;
}
修改虚拟主机3

基于端口

 1、修改虚拟主机文件。

# 基于端口 主机1
server{
   # 监听端口
   listen 80;
   # 访问域名
   server_name www.xxx.com;
   # 网页索引类型
   index index.html index.htm;
   # 指定URL资源
   root /usr/local/url1;
}
修改虚拟主机1
# 基于端口 主机2
server{
   # 监听端口
   listen 8080;
   # 访问域名
   server_name www.xxx.com;
   # 网页索引类型
   index index.html index.htm;
   # 指定URL资源
   root /usr/local/url2;
}
修改虚拟主机2
# 基于端口 主机3
server{
   # 监听端口
   listen 8090;
   # 访问域名
   server_name www.xxx.com;
   # 网页索引类型
   index index.html index.htm;
   # 指定URL资源
   root /usr/local/url3;
}
修改虚拟主机3

注意事项

1、加载php文件需要使用如下参数:

# 使用php类型
location ~ .php$ {
  include fastcgi_params;
        # TCP:127...:9000、也可以使用socket
  fastcgi_pass 127.0.0.1:9000;
        # 设置类型索引
  fastcgi_index index.php;
        # URL资源路径
  fastcgi_param SCRIPT_FILENAME /usr/local/url$fastcgi_script_name;
}
虚拟主机文件内

原文地址:https://www.cnblogs.com/xiangsikai/p/8393804.html