一个server模块配置多个虚拟站点

一个server模块配置多个虚拟站点

Nginx中的server_name指令主要用于配置基于名称虚拟主机,server_name指令在接到请求后的匹配顺序分别为:

(1)、准确的server_name匹配,例如:

server_name www.jason.com; 

(2)、以*通配符开始的字符串:

server_name *.jason.com; 

(3)、以*通配符结束的字符串:

server_name www.*; 

(4)、匹配正则表达式:

server_name ~^(?.+)\.jason\.com$; 

Nginx将按照1,2,3,4的顺序对servername进行匹配,只要有一项匹配成功后就会停止搜索。

官方解释如下:

During searching for a virtual server byname, if the name matches more than one of the specified variants, (e.g. both awildcard name and regular expression match), the first matching variant will bechosen, in the following order of priority:

  1. the exact name

  2. the longest wildcard name starting     with an asterisk, e.g. “*.example.com”

  3. the longest wildcard name ending with     an asterisk, e.g. “mail.*”

  4. the first matching regular expression     (in order of appearance in the configuration file)

在一个server块中配置多个站点

1、配置nginx

user nobody;

worker_processes  1;

pid nginx.pid;

error_log logs/error.log  notice;

worker_rlimit_nofile 65535;

 

events {

   worker_connections  65535;

   use epoll;

}

 

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"';

   log_format  download  '$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;

  client_max_body_size 20M;

  client_header_buffer_size    4k;

  large_client_header_buffers  4 8k;

  client_header_timeout  10;

  client_body_timeout    10;

  send_timeout           10;

  sendfile         on;

  tcp_nopush       on;

  tcp_nodelay      on;

  keepalive_timeout  60;

        

        

  gzip on;

  gzip_min_length  1k;

  gzip_http_version 1.1;

  gzip_buffers     4 8k;

  gzip_comp_level 2;

  gzip_types       text/plainapplication/x-javascript text/css application/xml;

  gzip_vary on;

 

   server {

       listen80; 

        server_name~^(www\.)?(.+)$; 

        access_loglogs/access.log main; 

       server_name_in_redirect off; 

  

        location / { 

                indexindex.html; 

                root/usr/local/nginx/html/$2; 

       }

 

       error_page  404              /404.html;

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

                        root   /usr/local/nginx/html;

       }

       

       location /status {

                stub_status on;

                access_log/usr/local/nginx/logs/status.log;

                auth_basic"NginxStatus";

                auth_basic_user_file/usr/local/nginx/conf/htpasswd;}

               

       location ~ /\.ht {

           deny  all;

       }

    }

 

}

备注:红色字体为多个站点配置。

2、  页面文件目录创建,站点的主目录应该类似于这样的结构:

/usr/local/nginx/html/test.org

/usr/local/nginx/html/test.com

/usr/local/nginx/html/Jason.org

如下创建

[root@jason1 html]# mkdir test.com test.orgjason.org

[root@jason1 html]# cd test.com/

[root@jason1 test.com]# ls

[root@jason1 test.com]# echo  "this is test.com" >>index.html

[root@jason1 test.com]# cd ..

[root@jason1 html]# cd test.org

[root@jason1 test.org]# echo "this istest.org">> index.html

[root@jason1 test.org]# cd ..

[root@jason1 html]# cd jason.org/

[root@jason1 jason.org]# echo "this isjason.org" >> index.html

[root@jason1 jason.org]#

3、启动nginx,设置域名与ip记录,进行访问测试。

wKioL1ZOehriSbrIAACcEMc51EQ916.jpg

wKioL1ZOehuhlTkDAACRwK8dgIQ566.jpg

wKioL1ZOehyTsK77AACikeNWV8Y572.jpg


你可能感兴趣的:(server,server_name)