Nginx网站服务详解(第二部分:Nginx服务的主配置文件 ——nginx.conf)

内容预知

1.  全局配置的六个模块简介

 2.nginx配置文件的详解

 2.1 全局配置模块

2.2 I/O 事件配置 

2.3 HTTP 配置 

 2.4 web服务监听设置

2.5 其他设置 

 3. 访问状态统计与控制

3.1 访问状态统计 

3.1 .1 查看访问统计配置的相关模块 

3.1.2 修改主配置文件,添加访问状态统计模块 

3.2 基于授权的访问控制 

3.2.1 生成用户密码认证文件 

3.2.2  修改主配置

3.2.3 重启服务,进行访问测试

3.3  基于客户端的访问控制

 4. Nginx的虚拟主机设置

 4.1 基于域名的虚拟主机

 4.1.1 域名准备和网页准备

4.1.2 主配置文件的修改 

4.1.3  重启服务,访问测试 

4.2  基于IP 的 Nginx 虚拟主机

4.2.1 设置虚拟主机IP 

4.2.2 修改主配置文件 

4.2.3 重启服务,访问测试

4.3 基于端口的 Nginx 虚拟主机 

 4.3.1 修改主配置文件

4.3.2 重启服务,测试访问测试


1.  全局配置的六个模块简介

  • 全局块:全局配置,对全局生效;
  • events块:配置影响 Nginx 服务器与用户的网络连接;
  • http块:配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置;
  • server块:配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块;
  • location块:用于配置匹配的 uri ;

注意:location 匹配的内容来源是来自网页的URI,而不是URL(URL代表整个链接如:www.baidu.com/images/search,而URI则是/images/search。所以nginx的location匹配的是URI)

  • upstream:配置后端服务器具体地址,负载均衡配置不可或缺的部分。

 2.nginx配置文件的详解

 2.1 全局配置模块

 就是配置文件从头开始到 events 块之间的内容,主要设置的是影响nginx服务器整体运行的配置指令。比如 worker_process,值越大,可以支持的并发处理量也越多,但是还是和服务器的硬件相关。

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

2.2 I/O 事件配置 

 

 #如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。

永久修改的方式:

[root@localhost init.d]#vim /etc/security/limits.conf 

注意:软硬件的事件处理都要设置才能生效,并且保存退出后,要重新连接查看才会生效 


#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。


#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。


#epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。(实现异步非阻塞)

2.3 HTTP 配置 

 2.4 web服务监听设置

2.5 其他设置 

 日志格式设定:
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

location常见配置指令,root、alias、proxy_pass
root(根路径配置):root /var/www/html
请求www.kgc.com/test/1.html,会返回文件/var/www/html/test/1.html

alias(别名配置):alias /var/www/html
请求www.yang.com/test/1.html,会返回文件/var/www/html/1.html

对比: 

当设置  location /test{     }

alias /var/www/html  和   root /var/www/html  有什么区别?

alias是别名设置,将设置的网页放在/var/www/html下,访问

root 是根目录设置 ,将设置的网页放在 /var/www/html/test 下,访问

proxy_pass(反向代理配置)

 

 3. 访问状态统计与控制

3.1 访问状态统计 

3.1 .1 查看访问统计配置的相关模块 

cat /opt/nginx-1.22.0/auto/options | grep YES   #可查看 nginx 已安装的所有模块

[root@localhost ~]#/usr/local/nginx/sbin/nginx -V
查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块

 

3.1.2 修改主配置文件,添加访问状态统计模块 

#主配置备份,防止设置错误,无法还原
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak

 修改主配配置操作:

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


 server {
        listen       80;
        server_name  www.yang.com;

        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /status {
            stub_status on;
            access_log off;
        }

 

重启nginx服务,访问测试:

 除此之外:还可以 curl -Ls http://192.168.73.105/status 结合 awk与if 语句进行性能监控。

3.2 基于授权的访问控制 

3.2.1 生成用户密码认证文件 

yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db

 

3.2.2  修改主配置

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

server {
		location / {
			......
			##添加认证配置##
			auth_basic "secret";				#设置密码提示框文字信息
			auth_basic_user_file /usr/local/nginx/passwd.db;
		}
	}

 

3.2.3 重启服务,进行访问测试

 

 

3.3  基于客户端的访问控制

设置方式类似于黑白名单

 设置前的访问,其他主机访问测试:

访问控制规则如下:


deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。

vim /usr/local/nginx/conf/nginx.conf
......
    server {
        location / {
            ......
            ##添加控制规则##
            allow 192.168.73.105;                     #允许访问的客户端 IP
            deny all;                                #拒绝其它IP客户端访问
        }
    }

 

设置后的访问测试:

 

 4. Nginx的虚拟主机设置

 相比较Apache的虚拟主机设置,Nginx的设置是十分简便的只需要修改主配置中的相关配置就能实现虚拟主机的效果 

 4.1 基于域名的虚拟主机

 4.1.1 域名准备和网页准备

[root@localhost conf]#echo "192.168.73.105 www.test1.com www.test2.com" >> /etc/hosts
[root@localhost conf]#mkdir -p /var/www/html/test1
[root@localhost conf]#mkdir -p /var/www/html/test2
[root@localhost conf]#echo "<h1>this is  test1</h1>" > /var/www/html/test1/index.html
[root@localhost conf]#echo "<h1>this is  test2</h1>" > /var/www/html/test2/index.html

4.1.2 主配置文件的修改 

vim /usr/local/nginx/conf/nginx.conf
http {
...... 
server {
        listen 80;
        server_name  www.test1.com;

        charset utf-8;

        access_log  logs/www.test1.access.log;

        location / {
            root /var/www/html/test1;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

    server {
        listen       80;
        server_name  www.test2.com;

        charset utf-8;

        access_log  logs/www.test2.access.log;

        location / {
            root /var/www/html/test2;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
..............
 }
}

 

4.1.3  重启服务,访问测试 

 

4.2  基于IP 的 Nginx 虚拟主机

4.2.1 设置虚拟主机IP 

[root@localhost conf]#ifconfig ens33:0 192.168.73.200/24
[root@localhost conf]#ifconfig ens33:0 

4.2.2 修改主配置文件 

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
    server {
        listen 192.168.73.105:80;
        server_name  www.test1.com;

        charset utf-8;

        access_log  logs/www.test1.access.log;

        location / {
            root /var/www/html/test1;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
     server {
        listen 192.168.73.200:80;
        server_name  www.test2.com;

        charset utf-8;

        access_log  logs/www.test2.access.log;

        location / {
            root /var/www/html/test2;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
..........
}

 

4.2.3 重启服务,访问测试

 

4.3 基于端口的 Nginx 虚拟主机 

 4.3.1 修改主配置文件

[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf

......
http {
......
    server {
        listen 192.168.73.105:666;
        server_name  www.test1.com;

        charset utf-8;

        access_log  logs/www.test1.access.log;

        location / {
            root /var/www/html/test1;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
     server {
        listen 192.168.73.105:888;
        server_name  www.test2.com;

        charset utf-8;

        access_log  logs/www.test2.access.log;

        location / {
            root /var/www/html/test2;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
..........
}

 

4.3.2 重启服务,测试访问测试

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值