1、nginx 稳定,由俄国人开发, nginx.org
分支: tengine,由淘宝开发; tengine.taobao.com
nginx的功能:http服务, 负载均衡 反向代理 邮件代理 缓存加速 SSL, 功能完善;高并发 可以用来处理动态请求;
yum 安装nginx;
1:首先添加nginx.repo 源;
[root@localhost_03 yum.repos.d]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@localhost_03 yum.repos.d]# yum list |grep nginx
[root@localhost_03 yum.repos.d]# yum list |grep nginx
nginx-filesystem.noarch 1:1.12.2-2.el7 @epel
nginx.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx
nginx-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-geoip.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-geoip-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-image-filter.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-image-filter-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-njs.x86_64 1:1.14.2.0.2.6-1.el7_4.ngx nginx
nginx-module-njs-debuginfo.x86_64 1:1.14.2.0.2.6-1.el7_4.ngx nginx
nginx-module-perl.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-perl-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-xslt.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-xslt-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-nr-agent.noarch 2.0.0-12.el7.ngx nginx
pcp-pmda-nginx.x86_64 4.1.0-5.el7_6 updates
2:然后使用yum 安装:
yum install -y nginx
systemctl start nginx start | stop | restart | reload
[root@localhost_03 yum.repos.d]# ps aux |grep nginx
root 1262 0.0 0.0 46364 964 ? Ss 16:40 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 1263 0.0 0.1 46764 1928 ? S 16:40 0:00 nginx: worker process
root 1265 0.0 0.0 112676 984 pts/0 S+ 16:40 0:00 grep --color=auto nginx
[root@localhost_03 yum.repos.d]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1262/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 864/sshd
tcp 0 0 0.0.0.0:56888 0.0.0.0:* LISTEN 864/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 945/master
tcp6 0 0 :::22 :::* LISTEN 864/sshd
tcp6 0 0 :::56888 :::* LISTEN 864/sshd
tcp6 0 0 ::1:25 :::* LISTEN 945/master
3: nginx -V 查看版本及参数;
[root@localhost_03 yum.repos.d]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
4:源码安装:自定义安装:用第三方模块 , 定制的模块等;
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar zxf nginx-1.14.0.tar.gz
cd nginx-1.14.0; ./configure --prefix=/usr/local/nginx
make && make install
/usr/local/nginx/sbin/nginx //启动
pkill nginx //杀死nginx进程,停止nginx服务
/usr/local/nginx/sbin/nginx -t //检测配置文件语法错误
/usr/local/nginx/sbin/nginx -s reload//重载配置
[root@localhost_03 src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
--2018-12-14 16:48:57-- http://nginx.org/download/nginx-1.14.0.tar.gz
长度:1016272 (992K) [application/octet-stream]
正在保存至: “nginx-1.14.0.tar.gz”
100%[===========================================================================================>] 1,016,272 92.9KB/s 用时 15s
2018-12-14 16:49:12 (66.3 KB/s) - 已保存 “nginx-1.14.0.tar.gz” [1016272/1016272])
[root@localhost_03 src]# tar -zxvf nginx-1.14.0.tar.gz
[root@localhost_03 src]# cd nginx-1.14.0
[root@localhost_03 nginx-1.14.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost_03 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
#### ./configure --help 查看简单编译参数说明;
[root@localhost_03 nginx-1.14.0]# make
[root@localhost_03 nginx-1.14.0]# make install
启动:
[root@localhost_03 nginx]# /usr/local/nginx/sbin/nginx
[root@localhost_03 nginx]# ps aux |grep nginx
root 7357 0.0 0.0 20544 612 ? Ss 17:26 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 7358 0.0 0.1 20988 1336 ? S 17:26 0:00 nginx: worker process
root 7360 0.0 0.0 112724 984 pts/1 R+ 17:27 0:00 grep --color=auto nginx
[root@localhost_03 nginx]# /usr/local/nginx/sbin/nginx -s reload
注释:源码文件在nginx/src/core里面;
查看nginx 的配置文件;
[root@localhost_03 nginx]# ls /etc/init.d/nginx
conf/ html/ logs/ sbin/
启动:/usr/local/nginx/sbin/nginx 注释:要是想重启,需要先用killall nginx 杀死进程后,再重启; killall (psmisc)
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
检测配置文件是否有错误: /usr/local/nginx/sbin/nginx -t
3:服务管理启动脚本,每次这样重启太麻烦了,可以在/etc/init.d/新建一个配置nginx的脚本;如下内容:
[root@localhost_03 html]# cat /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
并修改权限并设置开机启动: chmod 755 /etc/init.d/nginx
[root@localhost_03 html]# chmod 755 /etc/init.d/nginx
[root@localhost_03 html]# chkconfig --add nginx
[root@localhost_03 html]# chkconfig nginx --level 345 on
[root@localhost_03 html]# chkconfig --list|grep nginx
nginx 0:关 1:关 2:关 3:开 4:开 5:开 6:关
6: 重新启动nginx;
[root@localhost_03 html]# killall nginx
[root@localhost_03 html]# /etc/init.d/nginx restart
Reloading systemd: [ 确定 ]
Restarting nginx (via systemctl): [ 确定 ]
[root@localhost_03 html]# ps aux |grep nginx
root 7467 0.0 0.0 20544 608 ? Ss 17:38 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 7468 0.0 0.1 20984 1332 ? S 17:38 0:00 nginx: worker process
root 7470 0.0 0.0 112724 988 pts/1 R+ 17:38 0:00 grep --color=auto nginx
由上图可以看到主进程master是root用户, worker 是nobody用户启动的(在nginx的/conf/nginx.conf配置文件里定义)
7:nginx的配置文件详细:
主配置文件: /usr/local/nginx/conf/nginx.conf
[root@localhost_03 conf]# cat nginx.conf|grep -v '^$'
#user nobody; #ps aux里worker显示的用户;
worker_processes 1; #ps aux里显示的worker进程数;每个cpu定义一个进程即可; 也可以配置auto 自调整;
#error_log logs/error.log; #定义错误日记;日记级别:crit notice
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
##以上部分是全局部分:global
网络连接相关:
events { #事件相关
worker_connections 1024; #定义每个worker_process的最大连接数,最多只能这么多连接;
}
http协议相关:
http {
include mime.types;
default_type application/octet-stream;
#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; #应用之前的日记格式;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
虚拟主机相关: server 里 location相关:
server {
listen 80;
server_name localhost;
#charset koi8-r; #网站字符集;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
配置文件结构
全局配置(user、worker_processes、error_log、pid)
events(网络连接相关,worker_connections)
http(最重要的部分,大部分功能都放这里)
server(虚拟主机相关)
location(server里面)
全局配置项结构
https://coding.net/u/aminglinux/p/nginx/git/blob/master/3z/global.md
events配置项结构
https://coding.net/u/aminglinux/p/nginx/git/blob/master/3z/events.md
http配置项
https://coding.net/u/aminglinux/p/nginx/git/blob/master/3z/http.md
server配置项
https://coding.net/u/aminglinux/p/nginx/git/blob/master/3z/server.md
注释:nginx的错误日记级别:
错误日志级别:常见的错误日志级别有[debug|info|notice|warn|error|crit|alert|emerg],级别越高记录的信息越少。 如果不定义默认是error
accept_mutex on; events下:
当某一个时刻只有一个网络连接请求服务器时,服务器上有多个睡眠的进程会被同时叫醒,这样会损耗一定的服务器性能。
Nginx中的accept_mutex设置为on,将会对多个Nginx进程(worker processer)接收连接时进行序列化,防止多个进程争抢资源。
默认就是on。
multi_accept on; event下;
nginx worker processer可以做到同时接收多个新到达的网络连接,前提是把该参数设置为on。
默认为off,即每个worker process一次只能接收一个新到达的网络连接。
use epoll; 事件驱动器模型; nginx 模式用的;
Nginx服务器提供了多个事件驱动器模型来处理网络消息。
其支持的类型有:select、poll、kqueue、epoll、rtsing、/dev/poll以及eventport。
* select:只能在Windows下使用,这个事件模型不建议在高负载的系统使用
* poll:Nginx默认首选,但不是在所有系统下都可用
* kqueue:这种方式在FreeBSD 4.1+, OpenBSD2.9+, NetBSD 2.0, 和 MacOS X系统中是最高效的
* epoll: 这种方式是在Linux 2.6+内核中最高效的方式
* rtsig:实时信号,可用在Linux 2.2.19的内核中,但不适用在高流量的系统中
* /dev/poll: Solaris 7 11/99+,HP/UX 11.22+, IRIX 6.5.15+, and Tru64 UNIX 5.1A+操作系统最高效的方式
* eventport: Solaris 10最高效的方式