nginx——nginx配置虚拟主机

来源:互联网 发布:大学生网络家教平台 编辑:程序博客网 时间:2024/06/17 10:35

配置虚拟主机的目的主要是为了一台服务器访问多个网站

一.如何区分不同的网站:

1、域名不同

2、端口不同


二.通过域名区分虚拟主机

1.什么是域名

域名就是网站。
www.baidu.com
www.taobao.com
www.jd.com
Tcp/ip

Dns服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系。
一级域名:
Baidu.com
Taobao.com
Jd.com
二级域名:
www.baidu.com
Image.baidu.com
Item.baidu.com
三级域名:
1.Image.baidu.com
Aaa.image.baidu.com

一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。


2.如何修改域名

本地测试可以修改hosts文件。
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。


3.Nginx的配置

Nginx的配置文件:

/usr/local/nginx/conf/nginx.conf




#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 {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 {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}}server {listen 81;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html-81;index index.html index.htm;}}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;}}}

重新加载配置文件

[root@localhost nginx]# sbin/nginx -s reload



三.通过端口区分虚拟机
#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 {    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 {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }    }    server {        listen       81;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html-81;            index  index.html index.htm;        }    }}

重新加载配置文件

[root@localhost nginx]# sbin/nginx -s reload



原创粉丝点击