美文网首页Linux学习笔记
小白学nginx之配置nginx虚拟主机

小白学nginx之配置nginx虚拟主机

作者: 乔治大叔 | 来源:发表于2020-05-07 17:09 被阅读0次

Nginx配置虚拟主机有如下三种方式:

方式一、基于主机多IP方式

  1. 配置多网卡多IP的方式
[root@web01 conf.d]# cat ip.conf 
server {
    listen 10.0.0.7:80;
    server_name _;

    location / {
        root /code_ip_eth0;
        index index.html;
    }
}

server {
    listen 172.16.1.7:80;
    server_name _;

    location / {
        root /code_ip_eth1;
        index index.html;
    }
}
  1. 根据配置创建目录

mkdir /code_ip_eth0
echo "Eth0" > /code_ip_eth0/index.html
mkdir /code_ip_eth1
echo "Eth1" > /code_ip_eth1/index.html

  1. 重启nginx服务

systemctl restart nginx

  1. 使用curl命令测试

curl 172.16.1.7 Eth1
curl 172.16.1.7 Eth0

方式二、基于端口的配置方式

  1. 配置多端口的虚拟主机
[root@web01 conf.d]# vim port.conf
server {
        listen 81;
        location / { 
                root /code_81;
                index index.html;
        }
}
server {
        listen 82;
        location / { 
                root /code_82;
                index index.html;
        }
}  
  1. 根据配置文件创建所需的目录

mkdir /code_8{1..2}
echo "81" > /code_81/index.html
echo "82" > /code_82/index.html

3.检查语法并重启服务

[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
  1. 访问

http://10.0.0.7:82/

方式三、基于多个hosts名称方式(多域名方式)

  1. 准备多虚拟主机配置文件
[root@web01 conf.d]# cat test1.xxx.com.conf 
server {
    listen 80;
    server_name test1.xxx.com;

    location / {
        root /code/test1;
        index index.html;
    }
}

[root@web01 conf.d]# cat test2.xxx.com.conf 
server {
    listen 80;
    server_name test2.xxx.com;

    location / {
        root /code/test2;
        index index.html;
    }
}
  1. 根据配置文件创建对应的目录
[root@web01 conf.d]# mkdir /code/test{1..2} -p
[root@web01 conf.d]# echo "test1_server" > /code/test1/index.html
[root@web01 conf.d]# echo "test2_server" > /code/test2/index.html
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
  1. 配置域名解析
10.0.0.7      test1.xxx.com
10.0.0.7      test2.xxx.com
  1. 通过浏览器访问该网站
  2. nginx自查
1.修改完配置记得使用 nginx -t 检查语法
2.如果没有检查语法,直接重载导致报错。systemctl status nginx -l 查看错误信息

相关文章

  • Nginx 虚拟主机配置和日志管理 [2]

    一、nginx虚拟主机配置 Nginx虚拟主机对应的文件实体就是/usr/local/nginx/nginx.co...

  • nginx

    nginx的配置、虚拟主机、负载均衡和反向代理一nginx的配置、虚拟主机、负载均衡和反向代理二nginx的配置、...

  • 小白学nginx之配置nginx虚拟主机

    Nginx配置虚拟主机有如下三种方式: 方式一、基于主机多IP方式 配置多网卡多IP的方式 根据配置创建目录 mk...

  • Nginx——虚拟主机配置

    基于域名的虚拟主机 基于端口的虚拟主机 基于IP的虚拟主机nginx的主配置文件/etc/nginx/nginx....

  • Nginx

    安装Nginx 配置虚拟主机

  • Nginx location

    上一篇 Nginx 虚拟主机 Nginx location  在 nginx 的配置文件中,经常可以看到虚拟主机配...

  • nginx 403 forbidden

    首先确保虚拟主机路径配置正确 nginx django .conf 配置nginx python Django 集...

  • Nginx 常用配置

    1. Nginx 配置虚拟主机 1.1 基于 IP 的虚拟主机 修改配置文件 nginx.conf 重启服务 测试...

  • Nginx虚拟主机与安全认证实战

    知识要点: 配置Nginx虚拟主机实践 安全认证实践 安装验证 Nginx虚拟主机配置讲解 讲解配置之前我们要了解...

  • 【服务器部署】Linux+nginx+uwsgi+django服

    0x01 安装nginx 配置文件入口路径:/etc/nginx/nginx.conf;虚拟主机配置路径:/etc...

网友评论

    本文标题:小白学nginx之配置nginx虚拟主机

    本文链接:https://www.haomeiwen.com/subject/kxjubctx.html