main配置段:

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

worker_processes 4;    #worker的进程数;通常应该为CPU的核心数或核心数减1

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进程所能够打开的最大文件数量

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

events配置段:

events {

    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文件。

1、先在Nginx的默认路径下创建三个目录,也可以在其他指定路径下存放html文件:

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

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

2、分别在各自的路径下面写入如下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>

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

01、以win10为例

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

02、若要在Linux中访问:

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

3、基于域名的配置:在/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>

4、基于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>

5、基于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>