一、配置多个端口访问不同文件
#两个不同文件夹,分别存放不同文件
[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到下面,修改端口
[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;
}
}
}
二、配置不同域名访问不同文件
[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;
}
}
#正则表达式域名
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:WindowsSystem32driversetchosts“
#访问结果
三、配置不同域名访问同个文件
相同端口,不同域名 ,同个文件
[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
原文地址:https://blog.csdn.net/weixin_59128094/article/details/129422533
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_33800.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。