时间:2023-03-17来源:系统城装机大师作者:佚名
相同域名,不同端口,不同文件
1 2 3 4 5 6 7 |
#两个不同文件夹,分别存放不同文件 [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的站点指向一个文件夹,再复制这个server到下面,修改端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
[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; } } } |
#浏览器访问
相同端口,不同域名,不同文件
#四个文件夹,分别对应不同文件内容
1 2 3 4 5 6 7 |
[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 |
#修改配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
[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; } } #正则表达式域名 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 |
#配置宿主机host文件,在"C:\Windows\System32\drivers\etc\hosts"
#访问结果
sever_name匹配顺序:
相同端口,不同域名 ,同个文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[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再添加一个域名,不需要在复制一个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 |
#该宿主机的host文件
#访问结果如下:
到此这篇关于Nginx主机域名配置实现的文章就介绍到这了
2023-03-17
Nginx配置-日志格式配置方式2023-03-17
Apache APISIX Dashboard 未授权访问漏洞分析(CVE-2021-45232)2023-03-15
tomcat 启动时卡住问题排查及解决方法这篇文章主要介绍了Kubernetes中Nginx服务启动失败排查流程(Error: ImagePullBackOff),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以...
2023-03-15