nginx
-
安装
-
依赖
-
gcc 编译源码
-
pcre-devel 支持nginx.conf的正则
-
zlib-devel 压缩传输的依赖
-
默认自带模块
--without-select_module disable select module --without-poll_module disable poll module --without-http_charset_module disable ngx_http_charset_module --without-http_gzip_module disable ngx_http_gzip_module --without-http_ssi_module disable ngx_http_ssi_module --without-http_userid_module disable ngx_http_userid_module --without-http_access_module disable ngx_http_access_module --without-http_auth_basic_module disable ngx_http_auth_basic_module --without-http_autoindex_module disable ngx_http_autoindex_module --without-http_geo_module disable ngx_http_geo_module --without-http_map_module disable ngx_http_map_module --without-http_split_clients_module disable ngx_http_split_clients_module --without-http_referer_module disable ngx_http_referer_module --without-http_rewrite_module disable ngx_http_rewrite_module --without-http_proxy_module disable ngx_http_proxy_module --without-http_fastcgi_module disable ngx_http_fastcgi_module --without-http_uwsgi_module disable ngx_http_uwsgi_module --without-http_scgi_module disable ngx_http_scgi_module --without-http_memcached_module disable ngx_http_memcached_module --without-http_limit_conn_module disable ngx_http_limit_conn_module --without-http_limit_req_module disable ngx_http_limit_req_module --without-http_empty_gif_module disable ngx_http_empty_gif_module --without-http_browser_module disable ngx_http_browser_module --without-http_upstream_hash_module --without-http_upstream_ip_hash_module --without-http_upstream_least_conn_module --without-http_upstream_keepalive_module --without-http_upstream_zone_module --without-http disable HTTP server --without-http-cache disable HTTP cache --without-mail_pop3_module disable ngx_mail_pop3_module --without-mail_imap_module disable ngx_mail_imap_module --without-mail_smtp_module disable ngx_mail_smtp_module --without-stream_limit_conn_module disable ngx_stream_limit_conn_module --without-stream_access_module disable ngx_stream_access_module --without-stream_geo_module disable ngx_stream_geo_module --without-stream_map_module disable ngx_stream_map_module --without-stream_split_clients_module --without-stream_return_module disable ngx_stream_return_module --without-stream_upstream_hash_module --without-stream_upstream_least_conn_module --without-stream_upstream_zone_module --without-pcre disable PCRE library usage
-
其他模块介绍
--with-stream enable TCP/UDP proxy module --with-http_ssl_module enable ngx_http_ssl_module --with-http_stub_status_module enable ngx_http_stub_status_module
-
全局配置
http{ ... server{ # 定义虚拟主机 ... location / { # 发布目录 # auth_basic "用户认证"; # auth_basic_user_file /usr/local/nginx/pass; ... } } } ]# yum -y install httpd-tools ]# htpasswd -c /usr/local/nginx/pass admin
-
ssl虚拟主机
]#openssl genrsa > cert.key #生成私钥 ]#openssl req -new -x509 -key cert.key > cert.pem #生成证书
-
-
-
LNMP
-
安装
#nginx ]#yum -y install gcc pcre-devel openssl-devel ]#tar -xf nginx-1.12.2.tar.gz ]#cd nginx-1.12.2 ]#./configure --with-http_ssl_module ]#make && make install #mariadb ]#yum -y install mariadb-server mariadb mariadb-devel #php ]#yum -y install php php-fpm php-mysql #启动服务 ]#systemctl enable --now mariadb ]#systemctl enable --now php-fpm ]#/usr/local/nginx/sbin/nginx ]#vim /usr/local/nginx/conf/nginx,conf ... location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; } ... #测试php解析 ]#cat >> /usr/local/nginx/html/index.php <<EOF <?php $i="This is a php test Page\n" echo $i; ?> EOF #测试mysql ]#cat >> /usr/local/nginx/html/mysql.php <<EOF <?php $mysqli = new mysqli('localhost','root','','mysql'); //注意:root为mysql数据库的账户名称,密码需要修改为实际mysql密码,无密码则留空即可 //localhost是数据库的域名或IP,mysql是数据库的名称 if (mysqli_connect_errno()){ die('Unable to connect!'). mysqli_connect_error(); } $sql = "select * from user"; $result = $mysqli->query($sql); while($row = $result->fetch_array()){ printf("Host:%s",$row[0]); printf("</br>"); printf("Name:%s",$row[1]); printf("</br>"); } ?> EOF ]#/usr/local/nginx/sbin/nginx -s reload #测试 ]#curl www.zhaozhao.com/mysql.php ]#curl www.zhaozhao.com/index.php
-
FastCGI工作原理
- web server 启动时载入FastCGI进程管理器
- FastCGI进程管理器初始化,启动多个解释器进程
- 当客户端请求到达WebServer时,FastCGI进程管理器选择并连接到一个解释器
- FastCGI进程完成处理后返回结果,将标准输出和错误信息从同一连接返回WebServer
-
FastCGI缺点
- 因为是多进程,所以进程消耗更多的服务器内存,PHP-CGI解释器没进城消耗7-25M内存,将这个数字乘以50或者100就是很大的内存数
-
-
地址重写
-
获得一个来访的URL请求,然后改写成服务器可以处理的另一个URL的过程
-
优点
- 缩短URL,隐藏实际路径提高安全性
- 易于用户记忆和键入
- 易于被搜索引擎记录
-
语法
-
rewrite regex replacement flag
-
if (条件) {…}
-
正则表达式
区分大小写匹配 ~
不区分大小写不匹配 !~*
location / { ... rewrite /a.html /b.html; #隐藏实际路径 #域名跳转,域名跳转不隐藏实际路径 #rewrite ^/(.*) http://www.baidu.com/$1; }
-
-
rewrite选项
- flag: break, last, redirect, permanent
- last: 停止执行其他重写规则,地址栏不变
- break: 停止执行其他重写规则,完成本次请求
- redirect: 302临时重定向,地址栏改变
- permanent: 301永久重定向,地址栏改变
-
-
nginx反向代理
-
语法
http { ... upstream sergrp { #定义源服务器组 server 192.168.8.5:80 weight=2; server 192.168.8.6:80 down; server 192.168.8.6:80 max_fails=2 fail_timeout=30; } server { listen 80; server_name www.zhaozhao.com #WEB主机名 location / { ... proxy_pass http://sergrp; #调用服务组 } ... } } #默认轮询 ]#curl www.zhaozhao.com
-
调度算法
- 轮询
- weigth指定轮询几率
- ip_hash根据ip划分
-
服务器组主机状态
- down不参与负载
- max_fails允许请求失败的次数
- fail_timeout max_fails失败后,暂停提供服务的时间
-
ngx_stream_core_module模块 --with-stream开启该模块
-
语法
stream { upstream backend { server 192.168.8.10:21;#代理ftp服务器 } server { listen 12345; proxy_pass backend; } }
-
-
-
nginx优化
-
404页面
error 404 /404.html error_page 500 502 503 504 /50x.html location = /(50.html)|(404.html){ root html; }
-
常见状态码
- 200 一切正常
- 301 永久重定向
- 302 临时重定向
- 401 用户名密码错误
- 403 禁止访问(客户端IP地址被拒绝)
- 404 文件不存在
- 414 请求URL头部过长
- 500 服务器内部错误
- 502 BadGateway
-
查看访问状态信息
- 加载 --with-http_stub_status_module 模块
location /status { stub_status on; # allow ip地址; # allow ip地址; } ]#curl www.zhaozhao.com/status
-
优化访问量
worker_process 4;#与cpu核心数一致或2倍 events { worker_connections 65535; } ulimit -a#查看所有属性值 #临时设置硬限制和软限制 ulimit -Hn 100000 ulimit -Sn 100000 vim /etc/security/limits.conf#永久设置 ... #用户或组 硬限制软限制 需要限制的项目 限制的值 * soft nofile 100000 * hard nofile 100000
-
定义对静态页面的缓存时间
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { expires 30d; }
-
删除不需要的模块
]#./configure --without-http_autoindex_module --without-http_ssi_module --with-http_ssl_module --with-http_stub_status_module #重新编译安装
-
隐藏版本信息,开启传输压缩
http { server_tokens off; gzip on;#压缩传输方式可以更细分 }
-
限制并发量 (ngx_http_limit_req_module 模块)
http { limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server{ ... limit_req zone=one burst=5; } } #limit_req_zone语法格式如下: #limit_req_zone key zone=name:size rate=rate; #上面案例中是将客户端IP信息存储名称为one的共享内存,内存空间为10M #1M可以存储8千个IP信息,10M可以存储8万个主机连接的状态,容量可以根据需要任意调整 #每秒中仅接受1个请求,多余的放入漏斗 #漏斗超过5个则报错
-
拒绝非法请求(一般只用POST,GET)
if ($request_method !~ ^(GET|POST)$) { return 444; }
-
防止buffer溢出
#当客户端连接服务器时,服务器会启用各种缓存,用来存放连接的状态信息。 #如果攻击者发送大量的连接请求,而服务器不对缓存做限制的话,内存数据就有可能溢出(空间不足) http{ client_body_buffer_size 1k; client_header_buffer_size 1k; client_max_body_size 1k; large_client_header_buffers 2 1k; ... }
-