zoukankan      html  css  js  c++  java
  • nginx配置虚拟主机

    1.nginx 配置虚拟主机

    就是在一台服务器启动多个网站。

    如何区分不同的网站:

    1、域名不同

    2、端口不同

     

    1.1. 通过端口区分不同虚拟机

    Nginx的配置文件:nginx.conf

     1 worker_processes  1;
     2 
     3 events {
     4     worker_connections  1024;
     5 }
     6 
     7 
     8 http {
     9     include       mime.types;
    10     default_type  application/octet-stream;
    11 
    12     sendfile        on;
    13 
    14     keepalive_timeout  65;
    15 
    16     #一个server代表一个虚拟主机
    17     server {
    18         #监听的端口号
    19         listen       80;
    20     #服务名(也就是域名)
    21         server_name  localhost;
    22 
    23     #地址(默认/匹配所有的请求)
    24         location / {
    25         #请求转发到哪个目录下
    26             root   html;
    27         #首页是哪个(按顺序排)
    28             index  index.html index.htm;
    29         }
    30 
    31         error_page   500 502 503 504  /50x.html;
    32         location = /50x.html {
    33             root   html;
    34         }
    35     }
    36 
    37     server {
    38         #监听的端口号
    39         listen       81;
    40     #服务名(也就是域名)
    41         server_name  localhost;
    42 
    43     #地址(默认/匹配所有的请求)
    44         location / {
    45         #请求转发到哪个目录下
    46             root   html-81;
    47         #首页是哪个(按顺序排)
    48             index  index.html index.htm;
    49         }
    50 
    51         error_page   500 502 503 504  /50x.html;
    52         location = /50x.html {
    53             root   html;
    54         }
    55     }
    56 
    57     server {
    58         #监听的端口号
    59         listen       82;
    60     #服务名(也就是域名)
    61         server_name  localhost;
    62 
    63     #地址(默认/匹配所有的请求)
    64         location / {
    65         #请求转发到哪个目录下
    66             root   html-82;
    67         #首页是哪个(按顺序排)
    68             index  index.html index.htm;
    69         }
    70 
    71         error_page   500 502 503 504  /50x.html;
    72         location = /50x.html {
    73             root   html;
    74         }
    75     }
    76 
    77 }

    可以配置多个server,配置了多个虚拟主机。

    Nginx重新加载配置文件命令:

    nginx -s reload 

    然后通过配置的ip去访问就可以看到效果了。

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

    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地址可以被多个域名绑定。

     

    本地测试可以修改hosts文件。

    可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器(也就是远程服务了)。

     

     

    1.2.2. Nginx的配置

     1 #user  nobody;
     2 worker_processes  1;
     3 
     4 #error_log  logs/error.log;
     5 #error_log  logs/error.log  notice;
     6 #error_log  logs/error.log  info;
     7 
     8 #pid        logs/nginx.pid;
     9 
    10 
    11 events {
    12     worker_connections  1024;
    13 }
    14 
    15 
    16 http {
    17     include       mime.types;
    18     default_type  application/octet-stream;
    19 
    20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    21     #                  '$status $body_bytes_sent "$http_referer" '
    22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
    23 
    24     #access_log  logs/access.log  main;
    25 
    26     sendfile        on;
    27     #tcp_nopush     on;
    28 
    29     #keepalive_timeout  0;
    30     keepalive_timeout  65;
    31 
    32     #gzip  on;
    33 
    34     server {
    35         listen       80;
    36         server_name  localhost;
    37 
    38         #charset koi8-r;
    39 
    40         #access_log  logs/host.access.log  main;
    41 
    42         location / {
    43             root   html;
    44             index  index.html index.htm;
    45         }
    46     }
    47     server {
    48         listen       81;
    49         server_name  localhost;
    50 
    51         #charset koi8-r;
    52 
    53         #access_log  logs/host.access.log  main;
    54 
    55         location / {
    56             root   html-81;
    57             index  index.html index.htm;
    58         }
    59     }
    60     server {
    61         listen       80;
    62         server_name  www.taobao.com;
    63 
    64         #charset koi8-r;
    65 
    66         #access_log  logs/host.access.log  main;
    67 
    68         location / {
    69             root   html-taobao;
    70             index  index.html index.htm;
    71         }
    72     }
    73     server {
    74         listen       80;
    75         server_name  www.baidu.com;
    76 
    77         #charset koi8-r;
    78 
    79         #access_log  logs/host.access.log  main;
    80 
    81         location / {
    82             root   html-baidu;
    83             index  index.html index.htm;
    84         }
    85     }
    86 }

    192.168.25.148 www.taobao.com域名的配置:

    192.168.25.148 www.baidu.com

     

  • 相关阅读:
    83. Remove Duplicates from Sorted List
    141. Linked List Cycle
    hdu1028 划分数
    XDU1019 阶乘因子的个数
    poj2773 容斥原理
    poj1091 容斥原理的应用
    poj1173 多重集组合数
    HDU 1465 错排问题
    poj 1496
    复习之求一个数的约束之积模一个质数
  • 原文地址:https://www.cnblogs.com/itoyr/p/10071095.html
Copyright © 2011-2022 走看看