一,编译安装Nginx
1.yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel 安装依赖包
2.useradd -M -s /sbin/nologin nginx 新建Nginx用户,便于管理Nginx
3.cd /opt/
4.wget http://nginx.org/download/nginx-1.18.0.tar.gz
5.tar xf nginx-1.18.0.tar.gz 解压软件包
6.cd nginx-1.18.0/
7.mkdir /apps/nginx -p
8.
./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
9.make && make install
10./apps/nginx/sbin/nginx 绝对路径启动Nginx
二,location匹配
= 用于标准url前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求
^~ 用于标准url前,表示包含正则表达式,并且匹配以指定的正则表达式开头,对URI的最左边部分做匹配检查,不区分字符大小写
~ 用于标准url前,表示包含正则表达式,并且区分大小写
~* 用于标准url前,表示包含正则表达式,并且不区分大写
不带符号 匹配起始于此uri的所有的uri
三,nginx alias别名设置
server {
listen 80;
server_name www.kgc.com;
location /nwes {
root /data/nginx/html/pc/;
}
location /study{
alias /mnt/nginx/sports/;
}
}
nginx
yum install epel-release.noarch -y 安装 epel 源
yum install nginx -y 安装nginx
vim /etc/nginx/nginx.conf
http{
upstream web {
server 192.168.91.101;
server 192.168.91.102;
}
location / {
proxy_pass http://web;
}
systemctl start nginx
第三台机器 操作和第二台配置一样,修改tomcat服务器ip
yum install epel-release.noarch -y
yum install nginx -y
vim /etc/nginx/nginx.conf
location ~\.jps$ {
proxy pass http://第一台tomcat服务器ip:8080
}
location ~/.(jpg|html)$ {
root /usr/share/nginx/html;
}
两台tomcat服务器配置
设置jsp文件
安装tomcat
cd /usr/local/tomcat/webapps/ROOT/
mv index.jsp index.jsp.bak
echo "7-4" > index.jsp
真机网页搜索代理服务器的地址检测
四,自定义错误页面
www.pc.com/xxxxxxxx 404
www.pc.com/40x.html
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/html/pc;
error_page 404 /40x.html;
location = /40x.html {
root /data/nginx/html/pc/error/;
}
location / {
root /data/nginx/html/pc;
}
location /admin{
auth_basic "admin site";
auth_basic_user_file /apps/nginx/conf.d/.httpuser;
}
}
mkdir /data/nginx/html/pc/error/
cd /data/nginx/html/pc/error/
vim 40x.html 新建页面,名字需要和配置文件中的一致
自定义 错误码
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/html/pc;
error_page 404 =302 /40x.html;
location = /40x.html {
root /data/nginx/html/pc/error/;
}
location / {
root /data/nginx/html/pc;
}
location /admin{
auth_basic "admin site";
auth_basic_user_file /apps/nginx/conf.d/.httpuser;
}
}
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/html/pc;
error_page 404 =302 /index.html;
把错误码 404 指定成302 并跳到主页面:/index.html
五,rewrite重写功能。
location /main {
index index.html;
default_type text/html;
if ( $scheme = http ){
echo "if-----> $scheme";
}
if ( $scheme = https ){
echo "if ----> $scheme";
}
if (!-e $request_filename) {
echo "$request_filename is not exist";
#return ;
}
}
server {
listen 80;
server_name www.zzh.com;
root /data/nginx/pc/;
location / {
root /data/nginx/pc/;
}
location /test {
default_type text/plain;
return 301 http://www.baidu.com;
}
location /main {
index index.html;
default_type text/html;
if ( $scheme = http ){
return 666 "if-----> $scheme";
}
if (!-e $request_filename){
return 302 /index.html;
}
}
}
www.zzh.com/main/xxxxx
server {
listen 80;
server_name www.zzh.com;
root /data/nginx/pc/;
if (!-e $request_filename){
return 302 /index.html;
}
location / {
root /data/nginx/pc/;
}
location /test {
default_type text/plain;
return 301 http://www.baidu.com;
}
location /main {
index index.html;
default_type text/html;
if ( $scheme = http ){
return 666 "if-----> $scheme";
}
}
}
nginx调优
1.调整worker_processes和worker_connections参数,根据服务器的CPU核数和负载情况,设置合适的worker进程数量和每个进程的最大连接数。
2.启用缓存,Gzip压缩,HTTP2等功能,可以减少静态资源的传输量和响应时间,提高访问速度。
3.启用TCP缓冲,调整tcp_nodelay和tcp_nopush指令,可以优化TCP连接的效率和稳定性。
4.调整缓冲区大小,根据请求和响应的大小,设置合适的缓冲区大小,避免内存溢出或磁盘写入。
5.根据自己的用例,测试和调整Nginx的性能,一次只改变一个设置,观察性能的变化,保留有效的设置,还原无效的设置。
nginx内核参数调优
fs.file-max = 1000000
表示单个进程较大可以打开的句柄数
net.ipv4.tcp_tw_reuse = 1
参数设置为 1 ,表示允许将TIME_WAIT状态的socket重新用于新的TCP链接,这对于服务器来说意义重大,因为总有大量TIME_WAIT状态的链接存在
net.ipv4.tcp_keepalive_time = 600
当keepalive启动时,TCP发送keepalive消息的频度;默认是2小时,将其设置为10分钟,可更快的清理无效链接
net.ipv4.tcp_fin_timeout = 30
当服务器主动关闭链接时,socket保持在FIN_WAIT_2状态的较大时间
net.ipv4.tcp_max_tw_buckets = 5000
表示操作系统允许TIME_WAIT套接字数量的较大值,如超过此值,TIME_WAIT套接字将立刻被清除并打印警告信息,默认为8000,过多的TIME_WAIT套接字会使Web服务器变慢
net.ipv4.ip_local_port_range = 1024 65000
定义UDP和TCP链接的本地端口的取值范围
net.ipv4.tcp_rmem = 10240 87380 12582912
定义了TCP接受缓存的最小值、默认值、较大值
net.ipv4.tcp_wmem = 10240 87380 12582912
定义TCP发送缓存的最小值、默认值、较大值
net.core.netdev_max_backlog = 8096
当网卡接收数据包的速度大于内核处理速度时,会有一个列队保存这些数据包。这个参数表示该列队的较大值
net.core.rmem_default = 6291456
表示内核套接字接受缓存区默认大小
net.core.wmem_default = 6291456
表示内核套接字发送缓存区默认大小
net.core.rmem_max = 12582912
表示内核套接字接受缓存区较大大小
net.core.wmem_max = 12582912
表示内核套接字发送缓存区较大大小
注意:以上的四个参数,需要根据业务逻辑和实际的硬件成本来综合考虑
net.ipv4.tcp_syncookies = 1
与性能无关。用于解决TCP的SYN攻击
net.ipv4.tcp_max_syn_backlog = 8192
这个参数表示TCP三次握手建立阶段接受SYN请求列队的较大长度,默认1024,将其设置的大一些可使出现Nginx繁忙来不及accept新连接时,Linux不至于丢失客户端发起的链接请求
net.ipv4.tcp_tw_recycle = 1
这个参数用于设置启用timewait快速回收
net.core.somaxconn=262114
选项默认值是128,这个参数用于调节系统同时发起的TCP连接数,在高并发的请求中,默认的值可能会导致链接超时或者重传,因此需要结合高并发请求数来调节此值。
net.ipv4.tcp_max_orphans=262114
选项用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤立链接将立即被复位并输出警告信息。这个限制指示为了防止简单的DOS攻击,不用过分依靠这个限制甚至认为的减小这个值,更多的情况是增加这个值