需要先參考我的博客《Linux學(xué)習(xí)之Ubuntu 20.04在https://openresty.org下載源碼安裝Openresty 1.19.3.1,使用systemd管理OpenResty服務(wù)》安裝好Openresty
。
虛擬域名可以使用讓不同的域名訪問到同一臺主機(jī)。cd /usr/local/openresty
切換當(dāng)前訪問目錄到/usr/local/openresty
。
在/usr/local/openresty/nginx/conf/nginx.conf
文件的http配置塊末尾添加上以下的內(nèi)容:
server {
# 監(jiān)聽8000
listen 8000;
# 域名設(shè)為 www.sea.com
server_name www.sea.com;
location / {
# 訪問根目錄是html/sea
root html/sea;
index index.html index.htm;
}
}
server {
# 監(jiān)聽8800
listen 8800;
# 域名設(shè)為 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
文件整體內(nèi)容如下:
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 {
# 監(jiān)聽8000
listen 8000;
# 域名設(shè)為 www.sea.com
server_name www.sea.com;
location / {
# 訪問根目錄是html/sea
root html/sea;
index index.html index.htm;
}
}
server {
# 監(jiān)聽8800
listen 8800;
# 域名設(shè)為 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
進(jìn)行啟動。
lsof -i:8000
可以看到8000
端口已經(jīng)被監(jiān)聽,lsof -i:8800
可以看到8800
端口也已經(jīng)被監(jiān)聽,lsof -i:7800
沒有被監(jiān)聽,因?yàn)闆]有任何輸出,注意:7800端口只是測試一下,說明什么叫沒有被監(jiān)聽。
/usr/local/openresty/nginx/sbin/nginx
再次進(jìn)行啟動,發(fā)現(xiàn)報(bào)錯(cuò)如下:
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
的監(jiān)聽端口。
接下來把下邊的內(nèi)容也添加到/usr/local/openresty/nginx/conf/nginx.conf
http配置塊里:
server {
# 監(jiān)聽9800
listen 9800;
# 域名設(shè)為 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
文件整體內(nèi)容如下:
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 {
# 監(jiān)聽8000
listen 8000;
# 域名設(shè)為 www.sea.com
server_name www.sea.com;
location / {
# 訪問根目錄是html/sea
root html/sea;
index index.html index.htm;
}
}
server {
# 監(jiān)聽8800
listen 8800;
# 域名設(shè)為 www.sea.com
server_name www.side.com;
location / {
# 訪問根目錄是html/side
root html/side;
index index.html index.htm;
}
}
server {
# 監(jiān)聽9800
listen 9800;
# 域名設(shè)為 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
可以在不斷開已有連接的基礎(chǔ)上重新加載/usr/local/openresty/nginx/conf/nginx.conf
。
echo "127.0.0.1 www.sea.com www.side.com www.work.com" >> /etc/hosts
將127.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
創(chuàng)建三個(gè)目錄sea
、side
和work
。
執(zhí)行下邊命令:
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.com
的8000
端口。curl http://www.side.com:8800
可以訪問虛擬域名www.side.com
的8800
端口。curl http://www.work.com:9800
可以訪問虛擬域名www.work.com
的9800
端口。文章來源:http://www.zghlxwxcb.cn/news/detail-674686.html
此文章為8月Day 26學(xué)習(xí)筆記,內(nèi)容來源于極客時(shí)間《Linux 實(shí)戰(zhàn)技能 100 講》。文章來源地址http://www.zghlxwxcb.cn/news/detail-674686.html
到了這里,關(guān)于Linux學(xué)習(xí)之nginx虛擬域名主機(jī),lsof和netstat命令查看端口是否被監(jiān)聽的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!