Nginx主機(jī)域名配置實(shí)現(xiàn)
一、配置多個(gè)端口訪問不同文件
相同域名,不同端口,不同文件
#兩個(gè)不同文件夾,分別存放不同文件 [root@nginx ~]# mkdir /www/work_01 -p [root@nginx ~]# mkdir /www/work_02 [root@nginx ~]# vim /www/work_01/index.html this is work_01! [root@nginx ~]# vim /www/work_02/index.html this is work_02!
#編輯其中server模塊,把端口80的站點(diǎn)指向一個(gè)文件夾,再?gòu)?fù)制這個(gè)server到下面,修改端口
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#80端口,指向work_01的文件夾
server {
listen 80;
server_name localhost;
location / {
root /www/work_01;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#8080端口,指向work_02的文件夾
server {
listen 8080;
server_name localhost;
location / {
root /www/work_02;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}#瀏覽器訪問

二、配置不同域名訪問不同文件
相同端口,不同域名,不同文件
#四個(gè)文件夾,分別對(duì)應(yīng)不同文件內(nèi)容
[root@nginx ~]# cd /www/ [root@nginx www]# mkdir work_03 [root@nginx www]# mkdir work_04 [root@nginx www]# echo "This is work_03" > work_03/index.html [root@nginx www]# echo "This is work_04" > work_04/index.html [root@nginx www]# ls work_01 work_02 work_03 work_04
#修改配置文件
[root@nginx www]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
sendfile on;
keepalive_timeout 65;
#通配符在后的域名
server {
listen 80;
server_name www.haha.*;
location / {
root /www/work_01;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#精確域名
server {
listen 80;
server_name www.haha.com;
location / {
root /www/work_02;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#通配符在前的域名
server {
listen 80;
server_name *.haha.com;
location / {
root /www/work_03;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#正則表達(dá)式域名
server {
listen 80;
server_name ~\w+.com;
location / {
root /www/work_04;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@nginx www]# systemctl restart nginx#配置宿主機(jī)host文件,在"C:\Windows\System32\drivers\etc\hosts"

#訪問結(jié)果

sever_name匹配順序:
- 精準(zhǔn)匹配
- 通配符開頭,比如*.example.com
- 通配符結(jié)尾,比如www.example.*
- 正則表達(dá)式
- 默認(rèn)值
三、配置不同域名訪問同個(gè)文件
相同端口,不同域名 ,同個(gè)文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#只需要在server_name再添加一個(gè)域名,不需要在復(fù)制一個(gè)server_name
server {
listen 80;
server_name www.xixi.com www.qiqi.com;
location / {
root /www/work_01;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@nginx ~]# systemctl restart nginx#該宿主機(jī)的host文件

#訪問結(jié)果如下:

到此這篇關(guān)于Nginx主機(jī)域名配置實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx主機(jī)域名配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mac中使用Nginx實(shí)現(xiàn)80端口轉(zhuǎn)發(fā)8080端口
端口轉(zhuǎn)發(fā)(Port forwarding),有時(shí)被叫做隧道,是安全殼(SSH) 為網(wǎng)絡(luò)安全通信使用的一種方法。端口轉(zhuǎn)發(fā)是轉(zhuǎn)發(fā)一個(gè)網(wǎng)絡(luò)端口從一個(gè)網(wǎng)絡(luò)節(jié)點(diǎn)到另一個(gè)網(wǎng)絡(luò)節(jié)點(diǎn)的行為,其使一個(gè)外部用戶從外部經(jīng)過一個(gè)被激活的NAT路由器到達(dá)一個(gè)在私有內(nèi)部IP地址(局域網(wǎng)內(nèi)部)上的一個(gè)端口2017-09-09
Nginx基礎(chǔ)location語(yǔ)法及功能配置實(shí)例
這篇文章主要為大家介紹了Nginx基礎(chǔ)location語(yǔ)法及功能以及配置實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
nginx文件上傳限制以及超時(shí)無響應(yīng)問題的解決
小編最近開發(fā)前后端分離項(xiàng)目遇到一些問題,后端服務(wù)是基于80/443端口反向代理的,所以請(qǐng)求會(huì)經(jīng)過nginx網(wǎng)關(guān),然后將請(qǐng)求代理到后端服務(wù),這是開發(fā)環(huán)境,所以本文小編給大家介紹了解決nginx文件上傳限制和超時(shí)無響應(yīng),需要的朋友可以參考下2025-04-04
nginx安裝時(shí),make編譯可能會(huì)出現(xiàn)的錯(cuò)誤問題
這篇文章主要介紹了nginx安裝時(shí),make編譯可能會(huì)出現(xiàn)的錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

