赞
踩
目录
在企业信息化应用环境中。服务器的安全性和响应速度需要根据实际的情况进行相应的参数配置,达到最优的用户体验。默认的nginx安装参数只能提供最基本的服务,还需要调整如网页时间、连接超时、网页压缩等相应参数,才能发挥服务器的最大作用。
1-1、本地查看(查看头部信息)
1-2、浏览器查看
谷歌浏览器:更多工具→开发者工具→Network一刷新页面→点击ip地址→Headers→查看到版本
2-1、修改配置文件
- vim /usr/local/nginx/conf/nginx.conf
-
- events {
- worker_connections 1024;
- }
-
-
- http {
- include mime.types;
- default_type application/octet-stream;
- server_tokens off; ##添加命令,指的是关闭版本号
-
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
验证:
- systemctl restart nginx
- curl -i http://192.168.159.70
2-2、修改源码
- vim /opt/nginx-1.12.2/src/core/nginx.h
-
- #ifndef _NGINX_H_INCLUDED_
- #define _NGINX_H_INCLUDED_
-
-
- #define nginx_version 1012002
- #define NGINX_VERSION "8.0.0" #修改版本号
- #define NGINX_VER "mysql/" NGINX_VERSION #修改服务器类型
-
- #ifdef NGX_BUILD
重新编译:
- cd /opt/nginx-1.12.2/
- ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
- make && make install
- vim /usr/local/nginx/conf/nginx.conf
-
- ......
- http {
- include mime.types;
- default_type application/octet-stream;
- server_tokens on;
-
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- ......
-
-
- systemctl restart nginx
- curl -i http://192.168.159.70
若没有安装前创建用户,则在此服务中默认使用的是nobody
- vim /usr/local/nginx/conf/nginx.conf
-
- #user nobody;
- worker_processes 1;
- #默认是Nginx默认使用nobody用户账号与组账号
-
-
- #修改成如下内容
- user nginx nginx;
- worker_processes 1;
- systemctl restart nginx
- ps aux | grep nginx
当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
一般针对静态网页设置,对动态网页不设置缓存时间
- vim /usr/local/nginx/conf/nginx.conf
-
- server {
- listen 192.168.159.70:80;
- server_name www.accp.com;
- charset utf-8;
- access_log logs/accp.access.log;
- location / {
- root html;
- index index.html index.htm;
- } #输入下面4行内容
- location ~ \.(gif|jpg|png|bmp|ico)$ { ##加入新的location,以图片作为缓存对象
- root html;
- expires 1d; #指定缓存时间,1天
- }
- nginx -t
- systemctl restart nginx
- [root@localhost ~]# cd /usr/local/nginx/html
- [root@localhost html]# rz -E
- rz waiting to receive.
- [root@localhost html]# cd /usr/local/nginx/html
- [root@localhost html]# rz -E
- rz waiting to receive.
- [root@localhost html]# ls
- 50x.html index.html qiche.jpg
- [root@localhost html]# vim index.html
-
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <img src="qiche.jpg"/> #插入识别图片
-
- systemctl restart nginx

- [root@www ~]# vim /opt/fenge.sh
-
- #!/bin/bash
- #Filename:fenge.sh
- d=$(date -d "-1 day" "+%Y%m%d") #显示前一天的时间
- logs_path="/var/log/nginx"
- pid_path=" /usr/local/nginx/logs/nginx.pid"
- [ -d $logs_path ] || mkdir -p $logs_path #创建日志文件目录
- mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d#移动并重命名日志文件
- kill -HUP $(cat $pid_path) #重建日志文件
- find $logs_path -mtime +30 | xargs rm -rf #删除30天前的日志文件
-
- 首先先定义一个前一天的时间戳的变量d,再指定两个变量,一个是pid文件位置,一个是保存分割之后的日志文件位置,然后判断我们定义的/var/log/nginx是否存在,如果不存在创建,根据时间戳,在每天1点把前一天的日志给移动到我们新创建的$logs_path下,并且以前一天的时间戳命名,然后再去发送一个信号让我们的nginx重载,也就是生成一个新的日志文件,以此再去进行揭露当天的日志并且到第二天凌晨一点重新做这个事,最后设置30天清一次。
-
-
- [root@www ~]# chmod +x /opt/fenge.sh
- [root@www ~]# crontab -e
- crontab: no changes made to crontab
- [root@www ~]# crontab -l
- 0 1 * * * /opt/fenge.sh
- [root@www ~]# systemctl restart nginx
- [root@www ~]# netstat -natp | grep nginx
- tcp 0 0 192.168.159.100:80 0.0.0.0:* LISTEN 4094/nginx: master
- tcp 0 0 192.168.159.70:80 0.0.0.0:* LISTEN 4094/nginx: master
- [root@www ~]# sh -x /opt/fenge.sh
- ++ date -d '-1 day' +%Y%m%d
- + d=20211010
- + logs_path=/var/log/nginx
- + pid_path=' /usr/local/nginx/logs/nginx.pid'
- + '[' -d /var/log/nginx ']'
- + mkdir -p /var/log/nginx
- + mv /usr/local/nginx/logs/access.log /var/log/nginx/test.com-access.log-20211010
- ++ cat /usr/local/nginx/logs/nginx.pid
- + kill -HUP 4094
- + find /var/log/nginx -mtime +30
- + xargs rm -rf
- [root@www ~]# ls /var/log/nginx
- test.com-access.log-20211010
- [root@www ~]# date -s 20211010
- 2021年 10月 10日 星期日 00:00:00 CST

为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
Keepalive_timeout
设置连接保持超时时间
Client_header timeout
指定等待客户端发送请求头的超时时间
Client_body _timeout
设置请求体读超时时间
- [root@www ~]# vim /usr/local/nginx/conf/nginx.conf
-
- #keepalive_timeout 0;
- keepalive_timeout 65;
- client_header_timeout 80; #等待客户端发送请求头的超时时间,超时会发送408错误
- client_body_timeout 80; #等待客户端发送请求体的超时时间
-
-
- [root@www ~]# nginx -t
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- [root@www ~]# cat /proc/cpuinfo | grep -c "physical" #核心数
- 12
- [root@www ~]# ps aux | grep nginx #—一个主进程包含一个子进程
- root 4094 0.0 0.0 20644 1452 ? Ss 11:25 0:00 nginx: master process /usr/local/ngi
- nx/sbin/nginxnginx 4123 0.0 0.0 22956 1444 ? S 11:27 0:00 nginx: worker process
- root 4889 0.0 0.0 112680 980 pts/1 S+ 12:03 0:00 grep --color=auto nginx
- [root@www ~]# vim /usr/local/nginx/conf/nginx.conf
- #user nobody;
- worker_processes 2; #修改与cPU核数相同或2倍( cgroup)
- worker_cpu_affinity 01 10; #设置每个进程由不同的cpu处理、进程数配为2时,为0001 0010 0100 1000
-
- [root@www ~]# systemctl restart nginx
-
-
- PS:
- 01表示启用第一个cPu内核,10表示启用第二个cPu内核
- worker cpu affinity 0110;表示开启两个进程,第一个进程对应着第一个cP u内核,第二个进程对应着第二个cPt内核。###2核cpu,开启4个进程
- worker processes 4 ;
- worker cpu affinity 01 10 01 10;
- PS:开启了四个进程,它们分别对应着开启2个cPU内核###4个cpu,开启4个进程
- worker processes 4 ;
- worker cpu affinity 0001 0010 0100 1000 ;
- Ps:0001表示启用第一个cPu内核,0010表示启用第二个cPu内核,依此类推

- [root@www ~]# vim /usr/local/nginx/conf/nginx.conf
- #gzip on;
- gzip on; #取消注释,开启gzip压缩功能
- gzip_min_length 1k; #最小压缩文件大小
- gzip_buffers 4 16k; #压缩缓冲区,大小为4个16k缓冲区
- gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
- gzip_comp_level 6; #压缩比率
- gzip_vary on; #支持前端缓存服务器存储压缩页面
- gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;
- #支持前端缓存服务器存储压缩页面
-
- [root@www html]# vim index.html
- ......
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <img src="qiche.jpg"/> #网页中插入图片
-
-
- [root@www html]# systemctl restart nginx

在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络 ---> 选择 HTML、WS、其他
访问 http://192.168.184.20 ,双击200响应消息查看响应头中包含 Content-Encoding: gzip
服务端:192.168.159.80
盗链端:192.168.159.70
1-1、服务端配置
- [root@localhost ~]# cd /usr/local/nginx/html
- [root@localhost html]# rz -E
- rz waiting to receive.
- [root@localhost html]# ls
- 50x.html dog.jpg index.html
- [root@localhost html]# vim index.html
- <h1>Welcome to nginx!</h1>
- <img src="dog.jpg"/>
-
- [root@localhost html]# vim /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.159.70 www.dog.com
1-2、盗链端
- [root@www html]# cd /usr/local/nginx/html
- [root@www html]# ls
- 50x.html index.html qiche.jpg
- [root@www html]# vim index.html
- <h1>Welcome to nginx!</h1>
- <img src="http://www.dog.com/dog.jpg"/
- [root@www html]# vim /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.159.80 www.dog.com
2-1、服务端
- [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
-
- ......
- #gzip on
- ......
- location / {
- root html;
- index index.html index.htm;
- }
- location ~*\.(jpg|gif|swf)$ {
- valid_referers *.dog.com dog.com; #设置信任的访问来源
- if ( $invalid_referer ) {
- rewrite ^/ http://www.dog.com/error.png;
- #return 403;
- }
- }
-
-
- #error_page 404 /404.html;
- [root@localhost ~]# nginx -t
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- [root@localhost ~]# cd /usr/local/nginx/html
- [root@localhost html]# rz -E
- rz waiting to receive.
- [root@localhost html]# ls
- 50x.html dog.jpg error.png index.html
- [root@localhost html]# systemctl restart nginx
-
-
- 防盗链设置参数详细说明:
- valid referers:设置信任的网站,即能引用相应图片的网站none:浏览器中Referer为空的情况,就是直接在浏览器访问图片
- blocked:referer不为空的情况,但是值被代理或防火墙删除了,这些值不以以http:/l或者https://开头后面的网址或者域名:referer中包含相关字符串的网址
- if语句:如果链接的来源域名不在talid referers所列出的列表中,sinvalid referer为1,则执行后面的操作,即进行重写或返回403页面
- ##上传error.png文件至/usr/ local/ nginx/ html

-
- cd /usr/ local/php/etc/
- cp php-fpm.conf.default php-fpm.conf
- vim php-fpm.conf
- pid = run/php-fpm.pid
- vim /usr/local/php/etc/php-fpm.d/ www .conf
- #96行
- pm = dynamic #fpm进程启动方式,动态的
- #107行
- pm.max_children=20 #fpm进程启动的最大进程数
- #112行
- pm.start_servers = 5 #动态方式下启动时默认开启的进程数,在最小和最大之间
- #117行
- pm.min_spare_servers = 2 #动态方式下最小空闲进程数
- #122行
- pm. max_spare_servers = 8 #动态方式下最大空闲进程数
- #启动php-fpm,不可用于重启
- /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
- #执行第一个命令后,就可以使用下面这条命令查看pid号重启php-fpm
- kill -USR2 `cat /usr/ local/php/var/run/php-fpm.pid`
- netstat -anpt l grep 9000

Nginx可以作为正反向代理服务器,也可以作为负载均衡使用,而且是一个轻量级且高性能的服务器,所以肯定会出现高并发高流量的场景,而且安全问题一直处于被动状态,各种安全漏洞依然存在,为了服务器的正常运行,优化是非常重要的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。