Linux下安装和使用nginx
浏览器和服务器的关系

NGINX
nginx是什么
nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件。 nginx不但是一个优秀的web服务软件,还可以作为反向代理,负载均衡,以及缓存服务使用。 面试技巧: 支持高并发,能支持几万并发连接 资源消耗少,在3万并发连接下开启10个nginx线程消耗的内存不到200M 可以做http反向代理和负载均衡 支持异步网络i/o事件模型epoll
安装,启动nginx
0.解决依赖关系
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl- devel -y
1.下载源码包 wget -c https://nginx.org/download/nginx-1.12.0.tar.gz 2.解压缩源码 tar -zxvf nginx-1.12.0.tar.gz 3.配置,编译安装 开启nginx状态监测功能 ./configure --prefix=/opt/nginx-12/ --with-http_ssl_module --with-http_stub_status_module 编译且安装 make && make install 4.启动nginx,进入sbin目录,找到nginx启动命令 cd sbin ./nginx # 启动 ./nginx -s stop # 关闭 ./nginx -s reload # 重新加载
安装完成后检测服务
netstat -tunlp |grep 80
../sbin/nginx -t 检测nginx.conf配置文件的语法正确性 curl -i 127.0.0.1 # 如果访问不了,检查selinux,iptables
部署一个web站点
nginx默认站点是Nginx目录下的html文件夹,这里可以从nginx.conf中查到
location / {
root html; # 这里是默认的站点html文件夹,也就是/opt/nginx1-12/html/文件夹下的内容
index index.html index.html; # 站点首页文件名是index.html
Nginx的目录结构

-
conf存放nginx所有配置文件的目录,主要nginx.conf
-
html存放nginx默认站点的目录,如index.html、error.html等
-
logs存放nginx默认日志的目录,如error.log、access.log
-
sbin存放nginx主命令的目录,sbin/nginx
Nginx主配置文件解析
http内核模块
//公共的配置定义在http{}
http { //http层开始
...
//使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
'server' {
listen 80; //监听端口, 默认80
server_name localhost; //提供服务的域名或主机名
access_log host.access.log //访问日志
//控制网站访问路径
'location' / {
root /usr/share/nginx/html; //存放网站代码路径
index index.html index.htm; //服务器返回的默认页面文件
}
//指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton
error_page 500 502 503 504 /50x.html;
}
...
//第二个虚拟主机配置
'server' {
...
}
include /etc/nginx/conf.d/*.conf; //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件
} //http层结束
Nginx状态信息配置
启动status状态功能,修改配置文件
location /status {
# 开启nginx状态功能
stub_status on;
}
限制ip或ip段访问 禁止访问/av/底下的资源 location /av { deny 122.71.240.254; #alias /opt/nginx1-12/html/av; allow 10.1.1.0/16; }
平滑重启nginx
./sbin/nginx -s reload
访问status页面
http://192.168.119.10/status
通过ab压测命令检测
-n requests #执行的请求数,即一共发起多少请求。
-c concurrency #请求并发数。
-k #启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。
ab -kc 1000 -n 100000 http://192.168.119.10/
nginx访问日志功能:
1.在配置文件中,打开注释,../conf/nginx.conf
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
对应参数解析
$remote_addr 记录客户端ip $remote_user 远程用户,没有就是 “-” $time_local 对应[14/Aug/2018:18:46:52 +0800] $request 对应请求信息"GET /favicon.ico HTTP/1.1" $status 状态码 $body_bytes_sent 571字节 请求体的大小 $http_referer 对应“-” 由于是直接输入浏览器就是 - $http_user_agent 客户端身份信息 $http_x_forwarded_for 记录客户端的来源真实ip 97.64.34.118
配置多个域名的虚拟主机
修改nginx.conf文件
http {
include mime.types;
default_type application/octet-stream;
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 80;
server_name www.wjsqsmy.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location /status{
root /data/qsmy; # 目录可以自己创建
index index.html index.htm;
stub_status on;
}
# 可以优化错误信息页面
error_page 400 401 402 404 /40x.html; # 40x.html文件在qsmy文件夹下
}
server {
listen 80;
server_name www.wjshuoying.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /data/huoying;
index index.html index.htm;
}
error_page 400 401 402 404 /40x.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
监控访问信息
tail -f /opt/nginx1-12/logs/access.log

浙公网安备 33010602011771号