博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
搞了Apache,怎么可能不搞搞Nginx呢?
阅读量:3696 次
发布时间:2019-05-21

本文共 7697 字,大约阅读时间需要 25 分钟。

目录


一、概述

1.1.简介

     Nginx是一个轻量级/高性能的反向代理Web服务器,他实现非常高效的反向代理、负载平衡,他可以处理2-3万并发连接数,官方监测能支持5万并发,现在中国使用nginx网站用户有很多,例如:新浪、网易、 腾讯等。

1.2.优势

  • 占内存小,可实现高并发连接(3万~5万),处理响应快
  • 可实现http服务器、虚拟主机、方向代理、负载均衡
  • Nginx配置简单
  • 可以不暴露正式的服务器IP地址
  • 稳定性高
  • 系统消耗资源低
  • 节省宽带:支持GZIP压缩,可以添加浏览器本地缓存
  • 接收用户请求是异步的
  • Nginx内置的健康检查功能

1.3.缺点

  •  动态处理差:nginx处理静态文件好,耗费内存少,但是处理动态页面则很鸡肋,现在一般前端用nginx作为反向代理抗住压力

1.4.应用场景

  •      http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。
  •      虚拟主机。可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机。
  •      反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会应为某台  服务器负载高宕机而某台服务器闲置的情况。
  •      nginz 中也可以配置安全管理、比如可以使用Nginx搭建API接口网关,对每个接口服务进行拦截

二、安装配置

2.1.关闭防火墙、安全

systemctl stop firewalldsystemctl disable firewalldsetenforce 0iptables -F

2.2.解压并移至/opt文件夹下

tar xf  nginx-1.15.9.tar.gz  -C /opt

 2.3.编译初始化nginx

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module**********************省略make && make install

2.4.设置软链接

ln -s /usr/local/nginx/sbin/* /usr/local/bin

 2.5.添加用户

useradd -M -s /sbin/nologin nginx

 2.6.启动

nginx

三、配置分析

3.1.对nginx.pid的操作

#启动nginx后查看进程号ps -aux | grep nginxnetstat -natp | grep 80#未启动nginx时查看进程号cat /usr/local/nginx/logs/nginx.pid#杀死进程kill -l 
#退出nginxkill -s QUIT
#或者killall -3 nginxkillall -l QUIT nginx################################################重载方式#############kill -3
kill -s HUP
killall -l nginxkillall -s HUP

3.2.添加到service中管理

vim /etc/init.d/nginx#!/bin/bash# chkconfig: - 99 20							# chkcofig - “-” 表示不启用开机启动管理 (同时 若不加“#”, chkconfig add nginx 会加载不到配置)# description: Nginx Service Control ScriptCOM="/usr/local/nginx/sbin/nginx"				#命令程序文件位置(nginx)PID="/usr/local/nginx/logs/nginx.pid"			#pid文件case "$1" instart)   $COM   ;;stop)   kill -s QUIT $(cat $PID)   ;;restart)   $0 stop   $0 start   ;;reload)   kill -s HUP $(cat $PID)   ;;*)       echo "Usage: $0 {start|stop|restart|reload}"       exit 1esacexit 0
chmod +x /etc/init.d/nginxchkconfig --add nginx         #添加为系统服务systemctl stop nginxsystemctl start nginx

3.3.添加到 systemctl 中管理

vim /usr/lib/systemd/system/nginx.service[Unit]	Description=nginx							#描述After=network.target						#描述服务类别[Service]Type=forking								#后台运行类型PIDFile =/usr/local/nginx/logs/nginx.pid	#PID文件位置ExecStart=/usr/local/nginx/sbin/nginx		#启动服务ExecrReload=/bin/kill -s HUP $MAINPID		#根据PID重载配置ExecrStop=/bin/kill -s QUIT $MAINPID		#根据PID终止进程PrivateTmp=true[Install]WantedBy=multi-user.target					#启动级别chmod 754 /lib/systemd/system/nginx.service		#设置754权限是一种安全优化systemctl start nginx.servicesystemctl enable nginx.service

3.4.nginx主配置文件解析

worker_processes  1;                					# worker进程的数量events {                              					# 事件区块开始    worker_connections  1024;            				# 每个worker进程支持的最大连接数}                                    					# 事件区块结束http {                               					# HTTP区块开始    include       mime.types;            				# Nginx支持的媒体类型库文件    default_type  application/octet-stream;     		# 默认的媒体类型    sendfile        on;       							# 开启高效传输模式    keepalive_timeout  65;       						# 连接超时    server {            								# 第一个Server区块开始,表示一个独立的虚拟主机站点        listen       80;      							# 提供服务的端口,默认80        server_name  localhost;       					# 提供服务的域名主机名        location / {            						# 第一个location区块开始            root   html;       						# 站点的根目录,相当于Nginx的安装目录            index  index.html index.htm;      			# 默认的首页文件,多个用空格分开        }          										# 第一个location区块结果        error_page   500502503504  /50x.html;     		# 出现对应的http状态码时,使用50x.html回应客户        location = /50x.html {          				# location区块开始,访问50x.html            root   html;      							# 指定对应的站点目录为html        }    }

注:此处worker_connections  1024; 每个worker进程支持的最大连接数 和系统的文件打开数有一定关系,所以在优化的时候,需要通过ulimit -a查看文件打开数,如图

可以通过ulimit -n 65535 临时修改为最大文件打开值,那么如何永久修改呢,请看小节:3.5.永久修改文件打开最大值

3.5.永久修改文件打开最大值

#要想ulimits 的数值永久生效,必须修改配置文件/etc/security/limits.conf #在该配置文件中添加* soft nofile 65535   * hard nofile 65535  echo "* soft nofile 65535"  >> /etc/security/limits.confecho "* hard nofile 65535"  >> /etc/security/limits.conf* 表示所用的用户

3.6.配置访问统计

     通过/usr/local/nginx/sbin/nginx -V 查看HTTP_STUB_STATUS的模块是否存在,如果不存在,请重新./configure

#修改nginx.conf主配置文件#在server中,新增一个location,添加方式如下:location  /status {   stub_status on; #开启统计功能   access_log off; #关闭日志记录}

 访问方式:http://192.168.1.20/status

    其中三个数字表示:已经处理的连接数 、成功的TCP握手次数、已经处理的请求数

3.7.访问控制

  • 使用httpd-tools 工具进行对目录或者链接进行身份认证
  • -c:创建密码文件
yum install -y httpd-toolshtpasswd -c /usr/local/nginx/passwd. db zhangsan#添加nginx管理、给与400权限chown nginx /usr/ local/nginx/passwd.dbchmod 400 /usr/local/nginx/passwd.db#修改主配置文件相对应目录,添加认证配置项vim /usr/local/nginx/conf/nginx.conflocation /status {   auth_basic "secret";   auth_basic_user_file /user/local/nginx/passwd.db   ******************   省略}

3.8.基于客户端的访问限制

#修改主配置文件相对应目录,添加认证配置项vim /usr/local/nginx/conf/nginx.conflocation / {    root  html;    index  index.html index.htm;    deny 192.168.1.20; #限制192.168.1.20 访问    allow all; #放通其他来源的访问}

3.9.nginx的虚拟机主机

3.9.1.基于域名

  • 步骤1:修改/etc/hosts文件
echo "192.168.1.17 www.accp.com www.betnet" >> /etc/hosts
  • 步骤2:创建相对应用的index.html文件
mkdir  -p /var/www/html/accpmkdir  -p /var/www/html/benetecho "

www.accp.com

" >> /var/www/html/accp/index.htmlecho "

www.benet.com

" >> /var/www/html/benet/index.html
  • 步骤3:修改主配置文件nginx.conf
server {            						# 第一个Server区块开始,表示一个独立的虚拟主机站点        listen       192.168.1.17:80;      	# 提供服务的端口,默认80        server_name  www.accp.com;       		# 提供服务的域名主机名        charset utf-8;        access_log logs/accp.access.log;              location / {            				# 第一个location区块开始            root   /var/www/html/accp;       	# 站点的根目录,相当于Nginx的安装目录            index  index.html index.htm;      	# 默认的首页文件,多个用空格分开        }          								# 第一个location区块结果 server {            						        listen       192.168.1.17:80;      	        server_name  www.benet.com;        charset utf-8;        access_log logs/accp.access.log;              		        location / {            				            root   /var/www/html/benet;       	            index  index.html index.htm;              }

3.9.2.基于端口

  • 修改主配置文件
server {            						        listen       192.168.1.17:80;      	        server_name  www.accp.com;        charset utf-8;        access_log logs/accp.access.log;       		        location / {            				            root   /var/www/html/accp;                   index  index.html index.htm;             }          								 server {            						        listen       192.168.1.17:8080;      	        server_name  www.benet.com;        charset utf-8;        access_log logs/accp.access.log;             		        location / {            				            root   /var/www/html/benet;       	            index  index.html index.htm;              }

3.9.3.基于ipf访问

  • 步骤1:修改/etc/hosts
vim /etc/hosts192.168.1.17 www.accp.com192.168.1.100 www.benet.com
  • 步骤2:设置ip地址
ifconfig en33:0 192.168.1.100 netmask 255.255.255.0
  • 步骤3:修改主配置文件
server {            						        listen       192.168.1.17:80;      	        server_name  www.accp.com;        charset utf-8;        access_log logs/accp.access.log;             		        location / {            				            root   /var/www/html/accp;                   index  index.html index.htm;             }          								 server {            						        listen       192.168.1.100:80;      	        server_name  www.benet.com;          charset utf-8;        access_log logs/accp.access.log;           		        location / {            				            root   /var/www/html/benet;       	            index  index.html index.htm;              }

四、总结

       在整个部署,配置过程,大家可以清晰看出,Nginx作为静态访问强大的服务,其配置也是相对比较复杂的,但好在清晰,而Apache的配置相对来说就较为简单,使用与不使用都有总的主配置文件进行控制。当然Nginx在配置的灵活性上要高的多,毕竟可以在server中对各个页面进行做访问控制和功能模块添加。 

转载地址:http://vwucn.baihongyu.com/

你可能感兴趣的文章
zabbix图形中文字显示方框或乱码问题
查看>>
zabbix_agent5.0实现自动注册
查看>>
Centos7或redhat6.5系统openssh版本升级
查看>>
zabbix监控tomcat之Zabbix-java-gateway启动报错
查看>>
zabbix学习之路---------二进制包安装zabbix-agent
查看>>
zabbix学习之路---------zabbix监控nginx进程以及指标
查看>>
oracle-plsql执行脚本显示中文出现乱码
查看>>
keepalived实现Mysql高可用
查看>>
搭建redis及主从复制
查看>>
Spring Boot整合Thymeleaf模板
查看>>
Spring Boot整合FreeMarker模板
查看>>
IDEA如何自动生成 serialVersionUID 的设置
查看>>
git关联远程仓库--码云
查看>>
SpringBoot的单/多文件上传
查看>>
git常用命令大全
查看>>
SQL语句大全
查看>>
SpringBoot安全管理 ——模块1:Spring Security的基本配置
查看>>
SpringBoot安全管理 ——模块2:Spring Security 基于数据库的认证
查看>>
idea如何添加mybatis中的xml模板
查看>>
SpringBoot安全管理 ——模块3:OAuth 2的简单应用
查看>>