学习Nginx(五):虚拟主机配置

核心功能

在演示虚拟主机配置之前,来看一下Nginx配置的核心功能说明。

了解配置更多功能,请查看官方说明:

http://nginx.org/en/docs/ngx_core_module.html

[root@RockyLinux9 conf]# cat nginx.conf
# 核心功能,全局配置
# 设置启动nginx进程的用户
user  nginx;


# 设置工作进程的数量,一般设置为CPU个数*核数
worker_processes  2; 


# 指定错误日志的文件路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


# 指定pid文件路径
pid        logs/nginx.pid;


# 配置nginx连接的特性
events {
    # 配置工作进程的连接数
    worker_connections  1024;
}


...

虚拟主机配置

Nginx虚拟主机配置主要涉及在Nginx的配置文件中定义多个server块,每个server块代表一个虚拟主机,可以配置不同的域名、根目录等。

官方配置说明:

http://nginx.org/en/docs/http/request_processing.html

1. 基于IP的虚拟主机

在nginx正常安装启动后,我们就可以通过服务器的IP进行正常的访问,一般情况下配置无需更改。

http {  
    # ... 其他配置 ...  
  
    server {
        listen       80;
        server_name  localhost;


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


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    } 
  
    # ... 其他http块配置 ...  
}

2. 基于域名的虚拟主机配置

a. 修改配置文件,添加新的server配置段

[root@RockyLinux9 nginx]# pwd
/usr/local/nginx
[root@RockyLinux9 nginx]# vim conf/nginx.conf


http {  
    # ... 其他配置 ...  
    
    server {
        listen 80;
        server_name linuxjsz.com;


        location / {
                root html/linuxjsz.com;
                # 这里可以使用相对路径和绝对路径
                # 使用绝对路径可自定义其他目录
                #root /usr/local/nginx/html/linuxjsz.com;
                index index.html;
        }
    }
   
    # ... 其他http块配置 ...  
}

b. 重载配置文件

[root@RockyLinux9 nginx]# nginx -s reload

c. 创建对应的html文件

[root@RockyLinux9 nginx]# pwd
/usr/local/nginx
[root@RockyLinux9 nginx]# mkdir html/Linuxjsz.com
[root@RockyLinux9 nginx]# cat html/Linuxjsz.com/index.html
<html>
        This Web is Linuxjsz.com.
</html>

d. 通过IP访问Web

e. windows访问,修改hosts文件,添加域名解析

# 默认位置:C:\Windows\System32\drivers\etc


192.168.199.152 linuxjsz.com

f. 通过域名访问Web

3. 基于域名+端口的虚拟主机配置

a. 修改配置文件,添加新的server配置段

[root@RockyLinux9 nginx]# pwd
/usr/local/nginx
[root@RockyLinux9 nginx]# vim conf/nginx.conf


http {  
    # ... 其他配置 ...  
    server {
        listen 8080;
        server_name linuxjsz.com;


        location / {
                root /usr/local/nginx/html/linuxjsz8080.com;
                index index.html;
        }
    }
    # ... 其他http块配置 ...  
}

b. 重载配置文件

[root@RockyLinux9 nginx]# nginx -s reload

c. 创建对应的html文件

[root@RockyLinux9 nginx]# pwd
/usr/local/nginx
[root@RockyLinux9 nginx]# mkdir html/linuxjsz8080.com/
[root@RockyLinux9 nginx]# cat html/linuxjsz8080.com/index.html
<html>
        This Web is Linuxjsz's port 8080.
</html>

d. windows访问,修改hosts文件,添加域名解析

# 默认位置:C:\Windows\System32\drivers\etc


192.168.199.152 linuxjsz.com

e. 通过域名+端口访问Web

以上两种为Nginx虚拟主机的基本配置了,还有基于域名和IP的混合虚拟服务器配置,有需求可以查看官网,进行相关测试。

来自: 学习Nginx(五):虚拟主机配置

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux技术宅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值