Nginx 实战闲谈第4讲:Nginx配置虚拟主机

配置方式

Nginx配置虚拟主机,是实际应用中最基础的操作。你买了一台服务器,如果考虑并发或者系统的稳健性,这个是必须的技能,别产生依赖运维同事的不好习惯,实际工作中,nginx方面的问题也是必须掌握的。

虚拟主机配置方式:

1.基于多IP的方式

2.基于多端口的方式

3.基于多域名的方式

基于多IP的方式

一般情况下我们是基于域名进行配置,但是也不排除直接用IP进行配置的情况。比如你新买一个域名,单单购买认证 + 管局备案,没有20天是用不了域名的,管局审核真的是非常慢。所以在域名通过之前,还是可以直接通过IP进行配置,先用为爽。

1.第一个配置文件
[root@web02 /etc/nginx/conf.d]# vim mali.conf 
server {
    listen xx.xx.xx.xx:80;
    server_name localhost;
    location / {
        root /code/zhiwu;
        index index.html;
    }
}

2.第二个配置文件
[root@aqmh /etc/nginx/conf.d]# vim tank.conf 
server {
    listen yy.yy.yy.yy:80;
    server_name localhost;
    location / {
        root /code/tank;
        index index.html;
    }
}

3.检查配置重启
[root@aqmh /etc/nginx/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@aqmh /etc/nginx/conf.d]# systemctl restart nginx

备注:xx.xx.xx.xx和yy.yy.yy.yy直接替换成你的公网IP即可。

基于多端口的方式配置

1.第一个配置
[root@web02 /etc/nginx/conf.d]# cat mali.conf 
server {
    listen 80;
    server_name localhost;
    location / {
        root /code/zhiwu;
        index index.html;
    }
}

2.第二个配置
[root@web02 /etc/nginx/conf.d]# cat tank.conf 
server {
    listen 81;
    server_name localhost;
    location / {
        root /code/tank;
        index index.html;
    }
}

3.检查配置并重启
[root@aqmh /etc/nginx/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@aqmh /etc/nginx/conf.d]# systemctl restart nginx

4.访问测试
1)访问 http://xx.xx.xx.xx:80
2)访问 http://xx.xx.xx.xx:81

基于多域名的方式配置

1.第一个配置
[root@aqmh /etc/nginx/conf.d]# vim mali.conf 
server {
    listen 80;
    server_name xiaojiudun.club;
    location / {
        root /code/zhiwu;
        index index.html;
    }
}

2.第二个配置
[root@aqmh /etc/nginx/conf.d]# vim tank.conf 
server {
    listen 81;
    server_name xiaojiudun.club2;
    location / {
        root /code/tank;
        index index.html;
    }
}
3.检查并重启
[root@aqmh /etc/nginx/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@aqmh /etc/nginx/conf.d]# systemctl restart nginx


5.访问测试 省略

这样就可以通过不同的域名访问,比如xiaojiudun.club,这样访问xiaojiudun.club就是直接请求服务器的80端口,xiaojiudun.club2就是默认访问81端口。

好了,今天的内容就分享到这里,这个配置比较简单,一般沉下心,一会就可以配置完毕。欢迎一键三连,更多内容持续更新中。

更多精彩内容同步在"安前码后"公众号,打个星标防止走失。另外"韭盾"号年后上线,可以先关注,看一些跟打工人息息相关的东西,比如认知,又比如一起搞钱。

你可能感兴趣的:(Nginx实战集合,nginx,服务器)