vim /etc/rc.local //The script to automatically start the nginx program at boot, add nginx (/usr/local/nginx/sbin/nginx)
Deploy and implement backend web server
vim /usr/local/nginx/conf/nginx.conf
upstream server pool name {
ip_hash; // Set the same client to access the same web server
server server ip1:80 weight=2; //weight sets the server weight value, the default value is 1
server server ip2:80 max_fails=1 fail_timeout=10 down; //max_fails sets the maximum number of failures, fail_timeout sets the timeout in seconds, and down means that the service cannot be used normally.
................
}
location / {
proxy_pass http://server pool name; //Add a jump to the server pool at this location, it is a proxy server and does not need to do what the nginx server needs to do
}
Nginx's TCP/UDP scheduler
./configure \
--user=nginx //Specify the owner of the nginx installation as nginx
--group=nginx //Specify the group to which the nginx installation belongs to nginx
--with-http_ssl_module //Enable ssl encryption
--with-stream //--with-stream enables the four-layer reverse proxy function
make && make install //compile and install
vim /usr/local/nginx/conf/nginx.conf //Modify the main configuration file in the newly installed nginx environment.
................
worker_connections 1024;
}
stream {
upstream myssh {
server 192.168.2.100:22; //The ip and port number of the backend server;
server 192.168.2.200:22;
}
server {
listen 12345; //The port that nginx listens to;
proxy_pass myssh;
}
}
worker_processes 1; / / The number of processes is consistent with the number of cpu cores
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#
* soft nofile 100000
http {
................................
ssh -p 12345 192.168.4.5 //ssh -p 指定端口号 ssh nginx代理服务器 会直接连接后端服务器。
优化Nginx并发量
vim /usr/loca/nginx/conf/nginx.conf
#user nobody;
worker_connections 65535; //并发量默认值为1024,1024*cpu核数
//linux默认最大打开文件数量为1024
ulimit -a //查看所有属性值
ulimit -Hn 1000000 //设置硬限制(临时规则)
ulimit -Sn 1000000 //设置软限制(临时规则)
vim /etc/security/limits.conf //改配置文件设置永久
...............
#<domain> <type> <item> <value> //对谁设置 设置软限制还是硬限制 限制什么 数量
* hard nofile 100000
优化Nginx数据包头缓存
vim /usr/local/nginx/conf/nginx.conf
.................
http {
client_header_buffer_size 1k; //默认请求包头信息的缓存
large_client_header_buffers 4 4k; //大请求包头部信息的缓存个数与容量
浏览器本地缓存静态数据
vim /usr/local/nginx/conf/nginx.conf
................
location / {
root html;
index index.html index.htm;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires 30d; //定义客户端缓存时间为30天
}
自定义报错页面
200 一切正常
301 永久重定向
302 临时重定向
401 用户或密码错误
403 禁止访问(客户端ip地址被拒绝)
404 未找到(不存在)
414 URI太长(请求地址栏太长)
500 服务器内部错误
502 Bad Gateway
error_page 404 /404.html; //出现404报错则显示404.html内的内容
如何查看服务器状态信息
./configure \
> --with-http_ssl_module //开启SSL加密功能
> --with-strea //开启TCP/UDP代理模块
> --with-http_stub_status_module //开启status状态页面
vim /usr/local/nginx/conf/nginx.conf
… …
location /status {
stub_status on;
}
......
curl http://192.168.4.5/status
Active connections: 1
server accepts handled requests
10 10 3
Reading: 0 Writing: 1 Waiting: 0
Active connections:当前活动的连接数量。
Accepts:已经接受客户端的连接总数量。
Handled:已经处理客户端的连接总数量(一般与accepts一致,除非服务器限制了连接数量)。
Requests:客户端发送的请求数量。
Reading:当前服务器正在读取客户端请求头的数量。
Writing:当前服务器正在写响应信息的数量。
Waiting:当前多少客户端在等待服务器的响应。
对页面进行压缩处理
http {
.. ..
gzip on; //开启压缩
gzip_min_length 1000; //小文件不压缩
gzip_comp_level 4; //压缩比率
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
//对特定文件压缩,类型参考mime.types
.. ..
}