天天看点

nginx基础知识(掌握)

作者:软件开发资料查询网

前言

Nginx是一个高性能的HTTP和反向代理服务器,特点是占用内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。

nginx专为性能优化而开发,性能是其最重要的要求,十分注重效率,有报告nginx能支持高达50000个并发连接数

nginx安装

#nginx安装
yum -y install gcc pcre-devel openssl-devel			#依赖包
useradd -s /sbin/nologin  nginx						
./configure
--prefix=/usr/local/nginx							#指定安装目录
--user=nginx										#指定用户
--with-http_ssl_module								#开启加密功能
make && make install								#编译及安装           

nginx脚本启动

/usr/local/nginx/sbin/nginx							#启动
/usr/local/nginx/sbin/nginx	 -s stop				#关闭
/usr/local/nginx/sbin/nginx  -s reload				#从新加载配置
							-V						#查看软件信息
							-t						#测试配置文件           

nginx文件

/usr/local/nginx/html								#测试页面
#nginx配置文件
#Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log
#Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log
#PHP默认错误日志文件为/var/log/php-fpm/www-error.log
#-with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书
nginx/conf/nginx.conf
server {
		listen		1.1.1.1:80								#监听IP地址与端口
        listen       80;									#监听端口
        server_name  localhost;								#网站域名
#地址重写格式【总结】
#rewrite 旧地址 新地址 [选项];
#last 不再读其他rewrite
#break 不再读其他语句,结束请求
#redirect 临时重定向
#permanent 永久重定向
        rewrite  /a.html /b.html redirect;					#将a网页重定向到b网页并地址跳转
        rewrite ^/ http://www.baidu.com;					#访问此地址全部从定向到baidu
		rewrite ^/(.*)$  http://www.baidu.com/$1;			#访问此地址下面的网页从定向baidu
        ssl_certificate      cert.pem;         				#这里是证书文件
        ssl_certificate_key  cert.key;        				#这里是私钥文件
        auth_basic "Input Password:";                       #认证提示符信息
        auth_basic_user_file  "/usr/local/nginx/pass";      #认证的密码文件
        location / {
            root   html;									#指定网站根路径
            index  index.html index.htm;
        }
	#这里,~符号代表正则匹配,*符号代表不区分大小写
	if ($http_user_agent ~* firefox) {           		    #识别客户端firefox浏览器
	rewrite ^(.*)$  /firefox/$1;
	}
  }           

lnmp环境

#部署lnmp平台解决动态数据,动态数据为网站解析代码后返回数据
yum -y install gcc openssl-devel pcre-devel						#nginx依赖环境
yum -y install php php-mysql php-fpm							#php及依赖环境
yum -y install   mariadb   mariadb-server   mariadb-devel		#mariadb及依赖环境
systemctl start mariadb php-fpm

#php-fpm配置文件
/etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000											#php端口号
pm.max_children = 32											#最大进程数
pm.start_servers = 15											#最小进程数

#修改nginx配置文件
nginx/conf/nginx.conf
location / {
            root   html;
            index  index.php  index.html   index.htm;
#设置默认首页为index.php,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把默认首页index.php返回给用户
        }
 location  ~  \.php$  {
            root           html;
            fastcgi_pass   127.0.0.1:9000;    #将请求转发给本机9000端口,PHP解释器
            fastcgi_index  index.php;
            include        fastcgi.conf;       #加载其他配置文件
        }           

nginx实现web反向代理

一;高可用一台宕机,服务不会停,

二;负载均衡(轮询,哈希,权重,随机等)。 分布式;各个服务器执行不同功能,来完成一件事

/nginx/conf/nginx.conf
.. ..
http {
.. ..
#使用upstream定义后端服务器集群,集群名称任意(如webserver)
#使用server定义集群中的具体服务器和端口
upstream webserver {
#通过ip_hash设置调度规则为:相同客户端访问相同服务器
                ip_hash;
                server 192.168.2.100 weight=1 max_fails=1 fail_timeout=30;
                server 192.168.2.200 weight=2 max_fails=2 fail_timeout=30;
                server 192.168.2.101 down;
        }
#weight设置服务器权重值,默认值为1
#max_fails设置最大失败次数,测试服务器几次才确认服务器失败
#fail_timeout设置失败超时时间,单位为秒
#down标记服务器已关机,不参与集群调度
.. ..
server {
        listen        80;
        server_name  localhost;
            location / {
#通过proxy_pass将用户的请求转发给webserver集群
            proxy_pass http://webserver;
        }           

nginx的TCP/UDP 调度器 Nginx编译安装时需要使用--with-stream,开启ngx_stream_core_module模块

stream {
            upstream backend {
               server 192.168.2.100:22;           #后端SSH服务器的IP和端口
               server 192.168.2.200:22;
}
            server {
                listen 12345;                    #Nginx监听的端口
                 proxy_pass backend;
             }
}           

nginx常见问题

#自定义错误页面
nginx/conf/nginx.conf
.. ..
charset utf-8;                    #仅在需要中文时修改该选项
error_page   404  /404.html;      #自定义错误页面
.. ..
nginx/html/404.html			      #定义错错误页面
#200			一切正常
#301			永久从定向
#302			临时从定向
#401			用户或密码错误
#403			禁止访问(客户端IP地址被拒接)
#404			文件不存在
#414			请求URL头过长
#500			服务器内部错误
#501			bad gateway
           

查看服务器状态 编译安装时使用--with-http_stub_status_module开启状态页面模块

… …
location /status {
                stub_status on;
                 #allow IP地址;
                 #deny IP地址;
        }
… …
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:当前多少客户端在等待服务器的响应           

优化nginx并发量

ad -n 2000 -c 2000 http://192.168.1.10 		#ad高并发测试
nginx/conf/nginx.conf
.. ..
worker_processes  2;                    	#与CPU核心数量一致
events {
worker_connections 65535;       		    #每个worker最大并发连接数
}
.. ..
#优化Linux内核参数(最大文件数量)
ulimit -a									#查看所有属性值
ulimit -Hn	100000							#设置硬限制(临时规则)
ulimit -Sn	100000							#设置软限制(临时规则)
vim  /etc/security/limits.conf
  .. ..
*               soft    nofile            100000
*               hard    nofile            100000
#该配置文件分4列,分别如下:
#用户或组    硬限制或软限制    需要限制的项目   限制的值           

优化nginx数据包头缓存

.. ..
http {
client_header_buffer_size    1k;        #默认请求包头信息的缓存    
large_client_header_buffers  4 4k;      #大请求包头部信息的缓存个数与容量
.. ..
}           

浏览器本地缓存静态数据

server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires        30d;            #定义客户端缓存时间为30天
}
}           

继续阅读