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

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

全局块:全局配置,对全局生效;
events块:配置影响 Nginx 服务器与用户的网络连接;
http块:配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置;
server块:配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块;
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”命令临时修改本地每个进程可以同时打开的最大文件数。(linux中一切皆文件,可以打开多少文件就代表可以打开多少进程

永久修改的方式:

[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

alias、root、proxy_pass 路径问题

1.【alias】

别名配置,用于访问文件系统,在匹配到location配置的URL路径后,指向【alias】配置的路径

location /test/ 
{ 
alias /home/sftp/img/; 
}
location /test/aaa/
{ 
alias /home/sftp/img/; 
}
location /test/aaa/bbb/
{ 
alias /home/sftp/img/; 

即:请求/test/1.jpg、/test/aaa/1.jpg、/test/aaa/bbb/1.jpg(省略了协议与域名),将会返回文件/home/sftp/img/1.jpg。

注意alias 后面有没有“/” 要和location 后面“/” 保持一致,否则找不到资源文件

2.【root】

根路径配置,用于访问文件系统,在匹配到location配置的URL路径后,指向【root】配置的路径,并把location配置路径附加到其后。如:

location /test/ 
{ 
root /home/sftp/img/; 
}

即:请求/test/1.jpg(省略了协议与域名),将会返回文件/home/sftp/img/test/1.jpg,相较于alias,使用root会把/test/附加到根目录之后

3.【proxy_pass】

反向代理配置,用于代理请求,适用于前后端负载分离或多台机器、服务器负载分离的场景,在匹配到location配置的URL路径后,转发请求到【proxy_pass】配置的URL,是否会附加location配置路径与【proxy_pass】配置的路径后是否有"/"有关,有"/"则不附加,

proxy_pass 带“/”类似于alias


location /test/ 
{ 
proxy_pass http://127.0.0.1:8080/; 
}
location /test/aaa/
{ 
proxy_pass http://127.0.0.1:8080/; 
}
location /test/aaa/bbb/
{ 
proxy_pass http://127.0.0.1:8080/; 
}

在 tomcat的webapp/ROOT/ 放一个1.png图片

即:请求/test/1.jpg、/test/aaa/1.jpg、/test/aaa/bbb/1.jpg(省略了协议与域名),将会被nginx转发请求到http://127.0.0.1:8080/1.jpg(未附加/test/和/test子目录路径)
 

proxy_pass 不带“/”类似于root


location /test
{ 
proxy_pass http://127.0.0.1:8080; 
}
location /test/aaa
{ 
proxy_pass http://127.0.0.1:8080; 
}
location /test/aaa/bbb
{ 
proxy_pass http://127.0.0.1:8080; 
}

需要在 tomcat webapp/ROOT/创建aaa/bbb 目录 之后把1.png方式aaa和bbb目录中

即:请求/test/1.jpg,/test/aaa/1.jpg,/test/aaa/bbb/1.jpg(省略了协议与域名),将会被nginx转发请求到http://127.0.0.1:8080/test/1.jpg,http://127.0.0.1:8080/test/aaa/1.jpg,http://127.0.0.1:8080/test/aaa/bbb/1.jpg(附加/test/以及子目录路径)。
 

详细信息见一下博客

Nginx——location常见配置指令,alias、root、proxy_pass 路径问题_location alias_Michaelwubo的博客-CSDN博客

 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{,.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.50.13;                     #允许访问的客户端 IP
            deny all;                                #拒绝其它IP客户端访问
        }
    }

设置后的访问测试

 4. Nginx的虚拟主机设置

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

4.1 基于域名的虚拟主机

4.1.1 域名准备和网页准备

[root@localhost conf]#echo "192.168.50.14 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.50.100/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 修改主配置文件

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


http {
......
    server {
        listen 192.168.50.14: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.50.14: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 重启服务,测试访问测试

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值