nginx篇(一)
来源:互联网 发布:淘宝类似图片怎么搜索 编辑:程序博客网 时间:2024/06/06 01:24
Nginx是由C编写的一款较apache更轻量级的web服务器,同时它也可以用作http的反向代理服务器,邮件的代理服务器,tcp的代理服务器。通过查看http://www.netcraft.com/这个网站的Internet Data Mining版块可以看到目前世界上主流的web服务器各自的使用份额,其中可以看到nginx近几年来的不错表现。在我看来,相对apache的厚重来说,nginx就像反应敏捷的年轻小伙子和聪明伶俐的少女,干起活来效率高,反应快,而且能干的活还挺多,给人带来愉悦的心情。
为什么如此受欢迎?
亮点一:支持在线升级程序版本,在线更换日志,以及在线更换配置文件,即称之为热部署。
亮点二:低内存消耗,据测试,10000个keep-alive连接模式下的非活动连接仅消耗2.5M内存。
nginx的安装:
方法一:红帽并没有将nginx收录,到官网下载rpm包,或者本地同步nginx的yum源,直接yum安装
]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
而后使用yum安装
yum install nginx
方法二:tar包安装,
去官网下载tar包:
[root@localhost ~]#yum install pcre2-devel.x86_64 openssl-devel zlib-devel
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.9.10.tar.gz
[root@localhost src]# tar xf nginx-1.9.10.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# mkdir nginx
[root@localhost local]# cd nginx-1.9.10/
[root@localhost nginx-1.9.10]# yum install openssl-devel
[root@localhost nginx-1.9.10]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error_log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-select_module --with-poll_module --with-threads --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --http-proxy-temp-path=/tmp --http-uwsgi-temp-path=/etc/uwsgi --http-scgi-temp-path=/etc/scgi --with-mail --with-mail_ssl_module --with-pcre --with-debug
[root@localhost nginx-1.9.10]# make -j 4 && make install
]# /usr/local/nginx/sbin/nginx ///启动nginx进程
]# /usr/local/nginx/sbin/nginx -s stop ///关闭nginx进程
//设置nginx环境变量
[root@localhost nginx-1.9.10]# vim /etc/profile.d/nginx.sh
[root@localhost nginx-1.9.10]# cat !$
cat /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin
[root@localhost nginx-1.9.10]# source !$
source /etc/profile.d/nginx.sh
//启动nginx,ps查看可以看到默认一个master启用了一个worker,这个值在默认的配置文件里
[root@localhost nginx-1.9.10]# nginx
[root@localhost nginx-1.9.10]# ps aux|grep nginx
root 6548 0.0 0.0 47512 1124 ? Ss 16:48 0:00 nginx: master process nginx
nobody 6549 0.0 0.1 50020 1968 ? S 16:48 0:00 nginx: worker process
root 6551 0.0 0.0 112640 960 pts/0 S+ 16:49 0:00 grep --color=auto nginx
测试浏览器访问url: http://172.16.52.59/
nginx服务管理
启动: nginx
关闭|重载: nginx -s stop|reload
配置nginx:
nginx的配置页面分为三个部分: main block,event,http
core module:
//worker_processes是设置worker线程的数量,通常为cpu总cores减1
worker_processes 3;
//worker_cpu_affinity 设置cpu绑定,在1.9.10开始可以自动绑定
worker_cpu_affinity auto;
//worker_priority 设置worker的优先级,即nice值 [-20,19],默认为0
查看nice值:
[root@localhost nginx-1.9.10]# ps axo comm,pid,nice |grep nginxnginx 6548 0nginx 6699 0nginx 6700 0nginx 6701 0
http cores:
//error_page 这个模块式配置错误页面的,有两种方式:
eg1:error_page 404 /error_page/404.html ;
//下面这个要指定code值的时候一定要404空格,而后等于号后面接数字不能有空格
error_page 404 =260 /error_page/404.html ;
server { listen 8080; server_name www.b.com; root /web/vhosts/;index index.html index.htm; location /images/ {root /web/vhosts/; # error_page 404 =260 /error_page/404.html ;auth_basic "just a important page:g";auth_basic_user_file /web/vhosts/.htpasswd; # try_files test2.html test3.html /redict_page/chuju.jpg ; }
- 定义防盗链
listen 80;
server_name node31.huahualin.com;
root /data/web/nginx;
location ~* \.(gif|jpg|jpeg)$ {
root /data/web/nginx3/;
}
error_page 403 /403.html;
valid_referers none blocked server_names example.* *.example.com www.example.org/galeries/ ~\.google\.;
if ($invalid_referer) {
return 403;
}
}
- 基于主机名配置虚拟机
listen address[:port] [default_server] [ssl] [http2 | spdy]
listen port [default_server] [ssl] [http2 | spdy]
- 基于端口配置虚拟机
- 也可以基于ip:port,但是不常用
- 重定向图片
server {
listen 80;
server_name node31.huahualin.com;
root /data/web/nginx;
location ~* \.(gif|jpg|jpeg)$ {
root /data/web/nginx3/;
}
- 定义uri重定向,可实现seo搜索引擎优化
listen 80;
server_name node31.huahualin.com;
root /data/web/nginx;
location ~* \.(gif|jpg|jpeg)$ {
root /data/web/nginx3/;
rewrite ^/img/(.*)$ /images/$1 redirect;
}
error_page 404 /404.html;
regex:正则表达式,用于匹配用户请求的url;对应的目录不存在,也是会重写的
replacement:重写为的结果;
[flag]:
last:重写完成之后停止对当前uri的进一步处理,改为对新url的新一轮处理;类似continue,用户不知道重写的
break:重写完成之后停止当uri的处理,转向其后面的其它配置;
redirect:重写完成之后会返回客户端一个临时的重定向,由客户端对新的url重新发起请求(302);用户知道是重写的
permanent:重写完成之后会返回客户端一个永久的重定向,由客户端对新的url重新发起请求(301);
- if条件匹配
listen 80;
server_name node31.huahualin.com;
return https://www.baidu.com;
}
说明:if (condition) { ... }
条件判断,引用新的配置上下文;
condition:
比较表达式:
==,!=
~:模式匹配,区分字符大小写;
~*:模式匹配,不区分字符大小写;
!~:模式不匹配,区分字符大小写;
!~*: 模式不匹配,不区分字符大小写;
文件及目录判断:
-f, !-f:是否存在且为普通文件;且的反义就是或,!-f是否不存在或者不为普通文件
-d, !-d: 是否存在且为目录;
-e, !-e:是否存在;
-x, !-x:是否存在且可执行;
return code [text];
return code URL;
return URL;
立即停止对请求的uri的处理,并返回指定的状态码;
- 使用文本压缩,用时钟周期换大量的带宽,节约带宽
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_disable msie6;
gzip_min_length 2;
gzip_types image/gif image/jpg image/jpeg image/png text/plain text/css text/xml application/json application/xml application/java-script application/x-javascript;
server {
listen 80;
server_name node31.huahualin.com;
root /data/web/nginx;
location ~* \.(gif|jpg|jpeg)$ {
root /data/web/nginx3/;
rewrite ^/img/(.*)$ /images/$1 redirect;
}
error_page 404 /404.html;
location /admin {
root /data/web/nginx3;
auth_basic "Admin";
auth_basic_user_file /etc/nginx/.ngxhtpasswd;
}
- nginx篇一
- nginx篇(一)
- nginx负载均衡篇一、nginx安装
- Nginx实战进阶篇一
- nginx (一)
- nginx介绍(一) 简介篇
- nginx介绍(一) 简介篇
- nginx介绍(一) 简介篇
- Nginx教程(一) Nginx入门教程
- nginx(一) nginx详解
- [Nginx]Nginx简单使用(一)
- 一、一分钟认识Nginx
- Nginx安装(一)
- Nginx安装笔记(一)
- nginx源码阅读(一)
- nginx 安装 (一)
- 再谈nginx变量(一)
- Nginx使用指南(一)
- ListView setOnItemClickListen 方法无效
- 本地发博客的技巧
- hibernate之5大核心接口详解
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- Android Studio1.0升级到1.5编译报错的处理
- nginx篇(一)
- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString cont
- java时间转换
- Android 5.0 禁止使用隐式Intent来启动Service.异常:service intent must be explicit
- 通过Runtime类启动外部文件
- hibernate之xxx.hbm.xml配置详解
- js jsonp跨域请求实现
- win7 .net加解密模块出错,System.Reflection.TargetInvocationException
- hibernate之hibernate.cfg.xml详解