nginx虚拟主机三种模式的简单实现

main配置段:html

user nginx;  #指定用于运行worker进程的用户和组node

worker_processes 4;    #worker的进程数;一般应该为CPU的核心数或核心数减1nginx

worker_cpu_affinity 0001 0010 0100 1000;  进程绑定在CPU上的指定核上正则表达式

error_log /var/log/nginx/error.log; #错误日志存放路径服务器

pid /run/nginx.pid;   #nginx运行进程路径并发

worker_rlimit_nofile 51200;   #单个worker进程所可以打开的最大文件数量app

daemon on;   #是否以守护进程方式启动nginx进程负载均衡

events配置段:curl

events {tcp

    worker_connections 50000;   #每一个worker进程所可以打开的最大并发链接数量;可是不能大于worker_rlimit_nofile 数量

    use epoll;   #指明并发链接请求的处理方法

    accept_mutex on; #是否打开负载均衡锁,处理新的链接请求的方法;on意味着由worker轮流处理新请求,off意味着每一个新请求的到达都会通知worker进程。默认须要开启


}

http配置段:包含server配置段,其server配置段也能够放置于/etc/nignx/conf.d/目录下

http {

    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  /var/log/nginx/access.log  main;


    sendfile            on;

    tcp_nopush          on;

    tcp_nodelay         on;

    keepalive_timeout   65;

    types_hash_max_size 2048;


    include             /etc/nginx/mime.types;

    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

}


server配置段:能够再主配置文件/etc/nginx/nginx.conf中的http中配置,也能够在/erc/nginx/conf.d/分别建立本身的conf文件。

一、先在Nginx的默认路径下建立三个目录,也能够在其余指定路径下存放html文件:

mkdir /usr/share/nginx/{yuming,port,IP}

mkdir -p /lufei/root/{yuming,port,IP}

二、分别在各自的路径下面写入以下index.html的文件

域名网页:

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于域名的虚拟主机-lufei-yuming</h2>

<h3>www.lufei-yuming.com</h3>

</body>  

</html>



Port网页

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于端口的虚拟主机-lufei-port</h2>

<h3>www.lufei-port.com</h3>

</body>  

</html>



IP网页

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于IP的虚拟主机-lufei-IP</h2>

<h3>www.lufei-IP.com</h3>

</body>  

</html>

说明:当服务器都配置完成后,须要在本地主机上作地址和域名解析

0一、以win10为例

C:\WINDOWS\system32\drivers\etc路径下的hosts文件添加以下内容:192.168.1.72    www.lufei.com

0二、若要在Linux中访问:

在/etc/hosts中配置 192.168.1.72    www.lufei.com

三、基于域名的配置:在/etc/nginx/conf.d/下建立一个文件yuming.conf,配置完成后nginx -s reload 从新加载

server {        

listen 80;      #监听端口 80                                 

server_name www.lufei.com *.lufei.com lufei.com *.lufei.*;   #域名的配置,其书写格式可使用正则表达式。

location / {                    

#root    /lufei/root/yuming;      #根目录的的绝对路径配置  

root yuming;   # 相对路径,相对nginx根目录。nginx的默认路径:/usr/share/nginx(yum安装方式的)    

index index.html;     #默认主文件。默认跳转到index.html页面                 

}    

}

访问测试:

[root@localhost yuming]# curl www.lufei.com

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于域名的虚拟主机-lufei-yuming</h2>

<h3>www.lufei-yuming.com</h3>

</body>  

</html>

四、基于port的配置:在/etc/nginx/conf.d/下建立一个文件port.conf,配置完成后nginx -s reload 从新加载

[root@localhost yuming]# cat /etc/nginx/conf.d/port.conf 

server {        

listen 2225;      #监听端口 2225                                 

server_name www.lufei.com *.lufei.com lufei.com *.lufei.*;   #域名的配置,其书写格式可使用正则表达式。

location / {                

#root    /lufei/root/port;      #根目录的的绝对路径配置  

root port;   # 相对路径,相对nginx根目录。nginx的默认路径:/usr/share/nginx(yum安装方式的)    

index index.html;     #默认主文件。默认跳转到index.html页面                 

}    

}

访问测试:

[root@localhost yuming]# curl www.lufei.com:2225

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于端口的虚拟主机-lufei-port</h2>

<h3>www.lufei-port.com</h3>

</body>  

</html>

五、基于IP的配置:在/etc/nginx/conf.d/下建立一个文件IP.conf  ,配置完成后nginx -s reload 从新加载

server {        

listen 80;      #监听端口 80                                 

server_name 192.168.1.72;   #域名的配置,其书写格式可使用正则表达式。

location / {                

#root    /lufei/root/IP;      根目录的的绝对路径配置  

root IP;   # 相对路径,相对nginx根目录。nginx的默认路径:/usr/share/nginx(yum安装方式的)    

index index.html;     #默认主文件。默认跳转到index.html页面                 

}    

}

访问测试:

[root@localhost yuming]# curl 192.168.1.72

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基于IP的虚拟主机-lufei-IP</h2>

<h3>www.lufei-IP.com</h3>

</body>  

</html>

相关文章
相关标签/搜索