nginx 配置虚拟主机,实现在一个服务器可以访问多个网站的方法
2017-12-23 22:09
1101 查看
在一台服务器上,访问不同的网站
通常有两种区分方式:
1.通过监听的端口号
2.通过域名
1.通过端口访问不同的主机:
Nginx的配置文件:
/usr/local/nginx/conf/nginx.conf
Centos文件默认编码格式 latin1
查看编码格式的命令: :set fileencoding
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } ##一个http节点 http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #server 节点,即 你需要访问网站的配置 #一个server节点,就是一个虚拟主机 server { listen 80; #监听的端口号,访问网站 默认是80端口 server_name localhost; #即访问的域名 #charset koi8-r; #access_log logs/host.access.log main; location / { #定位 root html; #定位的是nginx根目录下的 html文件夹 index index.html index.htm; #设置网站首页 } } }
此时 可以配置多个server,也就是配置了不同的主机
添加虚拟主机:(通过端口号 区别)
server { listen 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-81; #nginx根目录下 新建的html81 文件夹 index index.html index.htm; `` }
编辑好文件之后,我们重新加载配置文件
通过命令: ./nginx -s reload
效果:
我们知道,当一个服务器上配置多个网站时,我们不可能通过端口号来区分它们,所以接下来 我需要通过域名来区分
2.通过域名区分不同的虚拟主机
什么是域名??
域名就是网址
例如:www.baidu.com
通常我们在访问域名的时候,我们需要通过dns服务器解析域名
Dns服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系。
一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。
本地测试可以修改hosts文件。
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器!!!!
在刚刚的nginx.conf文件下 继续配置:
server { listen 80; server_name www.taobao.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-taobao; index index.html index.htm; } } server { listen 80; server_name www.baidu.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-baidu; index index.html index.htm; } } }
域名的配置:
192.168.25.148 www.test.com
192.168.25.148 www.yiyou.com
重启nginx服务
观察下效果:
以上这篇nginx 配置虚拟主机,实现在一个服务器可以访问多个网站的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- 如何在已有网站的服务器上面,通过nginx配置虚拟主机来达到多个应用使用同一个服务器的效果
- Nginx虚拟主机配置实践之nginx访问同一个地址方法(二)
- Nginx实现多个站点使用一个端口(配置Nginx的虚拟主机)
- Nginx实现多个站点使用一个端口(配置Nginx的虚拟主机)
- 在一个nginx服务器上配置多个访问站点的方法
- Tomcat服务器域名访问方式配置方法(虚拟主机)
- Apache Tomcat配置虚拟主机的方法(不同域名绑定同一IP访问不同网站)
- Nginx实现多个站点使用一个端口(配置Nginx的虚拟主机)
- Nginx实现多个站点使用一个端口(配置Nginx的虚拟主机)
- 解决nginx作为前端代理默认访问一个80端口虚拟主机配置问题
- nginx 配置虚拟主机访问PHP文件 502错误的解决方法
- 配置tomcat虚拟路径,也可以作为单独图片服务器的实现方法
- 配置apache虚拟主机,实现在一台服务器上运行多个网站
- Nginx实现多个站点使用一个端口(配置Nginx的虚拟主机)
- Nginx提供网站服务应用包括(虚拟主机、用户访问控制、用户验证、nginx平滑升级、防盗链)的配置
- Nginx实现多个站点使用一个端口(配置Nginx的虚拟主机)
- Nginx下配置网站SSL实现https访问本站就是用的这方法
- 一个虚拟主机实现多域名多网站的方法---实测可行
- nginx 配置虚拟主机访问PHP文件 502错误的解决方法
- Nginx下配置网站SSL实现https访问本站就是用的这方法