您现在的位置是:首页 >其他 >Nginx网络服务网站首页其他

Nginx网络服务

R1chArd_TvT 2023-05-21 04:00:02
简介Nginx网络服务

网关服务器remote_addr记录客户端地址

后端服务器remote_addr记录网关地址

$remote_add记录上一个发送方地址

$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

location /status {

stub_status on;

access_log off;

}

Action connections:当前并发数,活动连接数

server accepts handled requests:是一个度量指标,用于追踪服务器所接受的客户端连接数量、成功处理的连接数量以及服务器处理的总请求数量。

基于授权的访问控制

生成用户密码认证文件

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

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location /abc {
            alias /var/www/abc;
            index index.html;
            auth_basic "Hello Hello Hello";
            auth_basic_user_file /usr/local/nginx/passwd.db;
}

        location /state {
            stub_status on;
            access_log off;
        }

 

修改主配置文件相对应目录,添加认证配置项

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

访问状态统计配置

1.先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的Nginx是否包含HTTP_STUB_STATUS模块

2.修改 nginx.conf配置文件,指定访问位置并添加stub_status配置

cd /usr/local/nginx/conf

cp nginx.conf nginx.conf.bak

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

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /state {
            stub_status on;
            access_log off;
        }

        #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;
        }

定时任务,每十秒监测一次并发数,超过10000警告:

while true

do

count=$(curl -Ls 192.168.179.24/state | grep 'Active connections' | awk '{print $3}')

if [$count -ge 10000]

then

echo "警告当前并发负载过高,当前并发数为:$count"

fi

sleep 10

done

通过两台虚拟机对192.168.179.21/state 进行同时连续访问

基于域名的虚拟主机:

server {

listen 80;

server_name IP;

charset utf-8;

access_log logs/IP.access.log;

location / {

root /var/www/html/xxx;

index index.html index.php;

}

error_page 500 502 503 504 /50x.html;

location = 50x.html{

root html;

}

}

 

 基于IP的虚拟主机:

    server {
        listen 192.168.179.21:80;
        server_name  www.accp.com;

        charset utf-8;

        access_log  logs/accp.com-access.log;

        location / {
            root   /var/www/abc;
            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.179.210:80;
        server_name  www.benet.com;

        charset utf-8;

        access_log  logs/benet.com-access.log;

        location / {
            root   /var/www/123;
            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;
        }
}

 

 此为hosts方法,如果要用域名访问,需要修改hosts文件:

 

 基于端口的虚拟主机:

server {
        listen 192.168.179.21:666;
        server_name  www.accp.com;

        charset utf-8;

        access_log  logs/accp.com-access.log;

        location / {
            root   /var/www/abc;
            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.179.21:888;
        server_name  www.benet.com;

        charset utf-8;

        access_log  logs/benet.com-access.log;

        location / {
            root   /var/www/123;
            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;
        }
}

 

 修改端口也需要修改hosts文件,如果以IP访问,要加上http,告诉以http进行解析(80为默认,http可写可不写)

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。