Linux学习之nginx虚拟域名主机,lsof和netstat命令查看端口是否被监听

需要先参考我的博客《Linux学习之Ubuntu 20.04在https://openresty.org下载源码安装Openresty 1.19.3.1,使用systemd管理OpenResty服务》安装好Openresty

虚拟域名可以使用让不同的域名访问到同一台主机。
cd /usr/local/openresty切换当前访问目录到/usr/local/openresty

/usr/local/openresty/nginx/conf/nginx.conf文件的http配置块末尾添加上以下的内容:

bash 复制代码
server {
        # 监听8000
        listen       8000;
        # 域名设为 www.sea.com
        server_name  www.sea.com;

        location / {
            # 访问根目录是html/sea
            root   html/sea;
            index  index.html index.htm;
        }
    }
server {
        # 监听8800
        listen       8800;
        # 域名设为 www.sea.com
        server_name  www.side.com;

        location / {
            # 访问根目录是html/side
            root   html/side;
            index  index.html index.htm;
        }
    }

/usr/local/openresty/nginx/conf/nginx.conf文件整体内容如下:

bash 复制代码
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    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;
        }
    }
    server {
        # 监听8000
        listen       8000;
        # 域名设为 www.sea.com
        server_name  www.sea.com;

        location / {
            # 访问根目录是html/sea
            root   html/sea;
            index  index.html index.htm;
        }
    }
    server {
        # 监听8800
        listen       8800;
        # 域名设为 www.sea.com
        server_name  www.side.com;

        location / {
            # 访问根目录是html/side
            root   html/side;
            index  index.html index.htm;
        }
    }
}

我使用的是vim编辑器,要是不知道vim编辑怎么使用,可以看一下我的博客

/usr/local/openresty/nginx/sbin/nginx -t可以检查一下格式和测试都是正常的。

/usr/local/openresty/nginx/sbin/nginx进行启动。

lsof -i:8000可以看到8000端口已经被监听,lsof -i:8800可以看到8800端口也已经被监听,lsof -i:7800没有被监听,因为没有任何输出,注意:7800端口只是测试一下,说明什么叫没有被监听。

/usr/local/openresty/nginx/sbin/nginx再次进行启动,发现报错如下:

bash 复制代码
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8800 failed (98: Address already in use)

/usr/local/openresty/nginx/sbin/nginx -s stop可以停止nginx/usr/local/openresty/nginx/sbin/nginx再次启动。

netstat -ntpl | grep nginx查看一下nginx的监听端口。

接下来把下边的内容也添加到/usr/local/openresty/nginx/conf/nginx.confhttp配置块里:

bash 复制代码
   server {
        # 监听9800
        listen       9800;
        # 域名设为 www.work.com
        server_name  www.work.com;

        location / {
            # 访问根目录是html/work
            root   html/work;
            index  index.html index.htm;
        }
    }

/usr/local/openresty/nginx/conf/nginx.conf文件整体内容如下:

bash 复制代码
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    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;
        }
    }


    server {
        # 监听8000
        listen       8000;
        # 域名设为 www.sea.com
        server_name  www.sea.com;

        location / {
            # 访问根目录是html/sea
            root   html/sea;
            index  index.html index.htm;
        }
    }


    server {
        # 监听8800
        listen       8800;
        # 域名设为 www.sea.com
        server_name  www.side.com;

        location / {
            # 访问根目录是html/side
            root   html/side;
            index  index.html index.htm;
        }
    }
    
   server {
        # 监听9800
        listen       9800;
        # 域名设为 www.work.com
        server_name  www.work.com;

        location / {
            # 访问根目录是html/work
            root   html/work;
            index  index.html index.htm;
        }
    }
}

/usr/local/openresty/nginx/sbin/nginx -s reload可以在不断开已有连接的基础上重新加载/usr/local/openresty/nginx/conf/nginx.conf


echo "127.0.0.1 www.sea.com www.side.com www.work.com" >> /etc/hosts127.0.0.1 www.sea.com www.side.com www.work.com写入/etc/hosts里边。

mkdir /usr/local/openresty/nginx/html/sea /usr/local/openresty/nginx/html/side /usr/local/openresty/nginx/html/work/usr/local/openresty/nginx/html创建三个目录seasidework

执行下边命令:

bash 复制代码
echo "hello sea" > /usr/local/openresty/nginx/html/sea/index.html # 在/usr/local/openresty/nginx/html/sea/index.html写入 hello sea
echo "hello side" > /usr/local/openresty/nginx/html/side/index.html # 在/usr/local/openresty/nginx/html/side/index.html写入 hello side
echo "hello work" > /usr/local/openresty/nginx/html/work/index.html # 在/usr/local/openresty/nginx/html/work/index.html 写入 hello work

curl http://www.sea.com:8000可以访问虚拟域名www.sea.com8000端口。
curl http://www.side.com:8800可以访问虚拟域名www.side.com8800端口。
curl http://www.work.com:9800可以访问虚拟域名www.work.com9800端口。

此文章为8月Day 26学习笔记,内容来源于极客时间《Linux 实战技能 100 讲》

相关推荐
likfishdn3 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean23 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=25 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
若云止水34 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
吃汤圆的抹香鲸1 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
sz66cm2 小时前
Linux基础 -- ARM 32 位架构动态注入代码技术文档
linux·arm开发·架构