官方网站:https://nginx.org/en/download.htmlphp
下载后解压(切记不能含有中文路径!!),文件结构如图(我解压的路径就有中文,记得拷贝放置于英文目录下便可!):html
两种方法:node
1) 直接双击该目录下的"nginx.exe",便可启动nginx服务器;linux
2) 命令行进入该文件夹,执行start nginx命令,也会直接启动nginx服务器。nginx
开浏览器,输入地址:http://localhost,访问页面,出现以下页面表示访问成功。web
启动服务:start nginx 退出服务:nginx -s quit 强制关闭服务:nginx -s stop 重载服务:nginx -s reload (重载服务配置文件,相似于重启,服务不会停止) 验证配置文件:nginx -t 使用配置文件:nginx -c "配置文件路径" 使用帮助:nginx -h
申明:基于CentOS 7系统。算法
模块依赖性Nginx须要依赖下面3个包vim
依赖包安装顺序依次为:openssl、zlib、pcre, 最后安装Nginx包。后端
openssl-fips-2.0.2.tar.gz zlib-1.2.7.tar.gz pcre-8.21.tar.gz nginx-1.12.2.tar.gz
[root@localhost wcw]# tar -zxvf openssl-fips-2.0.2.tar.gz [root@localhost wcw]# cd openssl-fips-2.0.2 [root@localhost openssl-fips-2.0.2]# ./config [root@localhost openssl-fips-2.0.2]# make [root@localhost openssl-fips-2.0.2]# make install
[root@localhost wcw]# tar -zxvf zlib-1.2.7.tar.gz [root@localhost wcw]# cd zlib-1.2.7 [root@localhost zlib-1.2.7]# ./configure [root@localhost zlib-1.2.7]# make [root@localhost zlib-1.2.7]# make install
[root@localhost wcw]# tar -zxvf pcre-8.21.tar.gz [root@localhost wcw]# cd pcre-8.21 [root@localhost pcre-8.21]# ./configure [root@localhost pcre-8.21]# make [root@localhost pcre-8.21]# make install
[root@localhost wcw]# tar -zxvf nginx-1.12.2.tar.gz [root@localhost wcw]# cd nginx-1.12.2 [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/install/nginx --with-pcre=../pcre-8.21 --with-zlib=../zlib-1.2.7 --with-openssl=../openssl-fips-2.0.2 [root@localhost nginx-1.12.2]# make [root@localhost nginx-1.12.2]# make install
请注意:"--with-xxx="的值是解压目录,而不是安装目录!浏览器
启动服务:nginx 退出服务:nginx -s quit 强制关闭服务:nginx -s stop 重载服务:nginx -s reload (重载服务配置文件,相似于重启,但服务不会停止) 验证配置文件:nginx -t 使用配置文件:nginx -c "配置文件路径" 使用帮助:nginx -h
此时能够为Nginx添加环境变量,以便操做服务。(>>如何添加Linux环境变量?)
检测是否安装成功:
[root@localhost wcw]# nginx -t
出现以下提示,表示安装成功。
或者,在浏览器地址输入"127.0.0.1"回车出现以下页面,则表示安装成功。
在项目使用中,使用最多的三个核心功能是静态服务器、反向代理和负载均衡。
这三个不一样的功能的使用,都跟Nginx的配置密切相关,Nginx服务器的配置信息主要集中在"nginx.conf"这个配置文件中,而且全部的可配置选项大体分为如下几个部分.
main # 全局配置
events { # 工做模式配置
}
http { # http设置
....
server { # 服务器主机配置(虚拟主机、反向代理等)
....
location { # 路由配置(虚拟目录等)
....
}
location path {
....
}
location otherpath {
....
}
}
server {
....
location {
....
}
}
upstream name { # 负载均衡配置
....
}
}
做为web服务器,http模块是nginx最核心的一个模块,配置项也是比较多的,项目中会设置到不少的实际业务场景,须要根据硬件信息进行适当的配置。
sendfile on:配置on让sendfile发挥做用,将文件的回写过程交给数据缓冲去去完成,而不是放在应用中完成,这样的话在性能提高有有好处 tcp_nopush on:让nginx在一个数据包中发送全部的头文件,而不是一个一个单独发 tcp_nodelay on:让nginx不要缓存数据,而是一段一段发送,若是数据的传输有实时性的要求的话能够配置它,发送完一小段数据就马上能获得返回值,可是不要滥用哦 keepalive_timeout 10:给客户端分配链接超时时间,服务器会在这个时间事后关闭链接。通常设置时间较短,可让nginx工做持续性更好 client_header_timeout 10:设置请求头的超时时间 client_body_timeout 10:设置请求体的超时时间 send_timeout 10:指定客户端响应超时时间,若是客户端两次操做间隔超过这个时间,服务器就会关闭这个连接 limit_conn_zone $binary_remote_addr zone=addr:5m :设置用于保存各类key的共享内存的参数, limit_conn addr 100: 给定的key设置最大链接数 server_tokens:虽然不会让nginx执行速度更快,可是能够在错误页面关闭nginx版本提示,对于网站安全性的提高有好处哦 include /etc/nginx/mime.types:指定在当前文件中包含另外一个文件的指令 default_type application/octet-stream:指定默认处理的文件类型能够是二进制 type_hash_max_size 2048:混淆数据,影响三列冲突率,值越大消耗内存越多,散列key冲突率会下降,检索速度更快;值越小key,占用内存较少,冲突率越高,检索速度变慢
access_log logs/access.log:设置存储访问记录的日志 error_log logs/error.log:设置存储记录错误发生的日志
ssl_protocols:指令用于启动特定的加密协议,nginx在1.1.13和1.0.12版本后默认是ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2,TLSv1.1与TLSv1.2要确保OpenSSL >= 1.0.1 ,SSLv3 如今还有不少地方在用但有很多被攻击的漏洞。
ssl prefer server ciphers:设置协商加密算法时,优先使用咱们服务端的加密套件,而不是客户端浏览器的加密套件
gzip 是告诉nginx采用gzip压缩的形式发送数据。这将会减小咱们发送的数据量。 gzip_disable 为指定的客户端禁用gzip功能。咱们设置成IE6或者更低版本以使咱们的方案可以普遍兼容。 gzip_static 告诉nginx在压缩资源以前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而容许你使用最高压缩比,这样nginx就不用再压缩这些文件了(想要更详尽的gzip_static的信息,请点击这里)。 gzip_proxied 容许或者禁止压缩基于请求和响应的响应流。咱们设置为any,意味着将会压缩全部的请求。 gzip_min_length 设置对数据启用压缩的最少字节数。若是一个请求小于1000字节,咱们最好不要压缩它,由于压缩这些小的数据会下降处理此请求的全部进程的速度。 gzip_comp_level 设置数据的压缩等级。这个等级能够是1-9之间的任意数值,9是最慢可是压缩比最大的。咱们设置为4,这是一个比较折中的设置。 gzip_type 设置须要压缩的数据格式。上面例子中已经有一些了,你也能够再添加更多的格式。
open_file_cache 打开缓存的同时也指定了缓存最大数目,以及缓存的时间。咱们能够设置一个相对高的最大时间,这样咱们能够在它们不活动超过20秒后清除掉。
open_file_cache_valid 在open_file_cache中指定检测正确信息的间隔时间。
open_file_cache_min_uses 定义了open_file_cache中指令参数不活动时间期间里最小的文件数。
open_file_cache_errors 指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。咱们也包括了服务器模块,这些是在不一样文件中定义的。若是你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。
srever模块配置是http模块中的一个子模块,用来定义一个虚拟访问主机,也就是一个虚拟服务器的配置信息。
server { listen 80; server_name localhost 192.168.1.100; charset utf-8; access_log logs/access.log; error_log logs/error.log; ...... }
location模块是Nginx配置中出现最多的一个配置,主要用于配置路由访问信息。
在路由访问信息配置中关联到反向代理、负载均衡等等各项功能,因此location模块也是一个很是重要的配置模块。
location / { root /nginx/www; index index.php index.html index.htm; }
经过反向代理代理服务器访问模式,经过proxy_set配置让客户端访问透明化。
location / { proxy_pass http://localhost:8888; proxy_set_header X-real-ip $remote_addr; proxy_set_header Host $http_host; }
location / { include uwsgi_params; uwsgi_pass localhost:8888; }
upstream模块主要负责负载均衡的配置,经过默认的轮询调度方式来分发请求到后端服务器。简单的配置方式以下。
upstream name { ip_hash; server 192.168.1.100:8000 weight=9; server 192.168.1.100:8001 down; server 192.168.1.100:8002 max_fails=3; server 192.168.1.100:8003 fail_timeout=20s; server 192.168.1.100:8004 max_fails=3 fail_timeout=20s; }
首先,Nginx是一个HTTP服务器,能够将服务器上的静态文件(如HTML、图片)经过HTTP协议展示给客户端。
配置:
server { listen 80; # 端口 server_name localhost 192.168.1.100; # 域名 location / { # 表明这是项目根目录 root /usr/share/nginx/www; # 虚拟目录 } }
什么是反向代理?
客户端原本能够直接经过HTTP协议访问某网站应用服务器,若是网站管理员在中间加上一个Nginx,客户端请求Nginx,Nginx请求应用服务器,而后将结果返回给客户端,此时Nginx就是反向代理服务器。
反向代理配置:
server { listen 80; location / { proxy_pass http://192.168.0.112:8080; # 应用服务器HTTP地址 } }
既然服务器能够直接HTTP访问,为何要在中间加上一个反向代理,不是画蛇添足吗?反向代理有什么做用?继续往下看,下面的负载均衡、虚拟主机,都基于反向代理实现,固然反向代理的功能也不只仅是这些。
当网站访问量很是大,也摊上事儿了。由于网站愈来愈慢,一台服务器已经不够用了。因而将相同的应用部署在多台服务器上,将大量用户的请求分配给多台机器处理。同时带来的好处是,其中一台服务器万一挂了,只要还有其余服务器正常运行,就不会影响用户使用。Nginx能够经过反向代理来实现负载均衡。
负载均衡配置:
upstream myapp {
ip_hash; # 固定访客 server 192.168.0.111:8080 weight=9; # 应用服务器1 server 192.168.0.112:8080 weight=1; # 应用服务器2 }
有的网站访问量大,须要负载均衡。然而并非全部网站都如此出色,有的网站,因为访问量过小,须要节省成本,将多个网站部署在同一台服务器上。
例如将www.aaa.com和www.bbb.com两个网站部署在同一台服务器上,两个域名解析到同一个IP地址,可是用户经过两个域名却能够打开两个彻底不一样的网站,互相不影响,就像访问两个服务器同样,因此叫两个虚拟主机。
虚拟主机配置:
server { listen 80 default_server; server_name _; return 444; # 过滤其余域名的请求,返回444状态码 } server { listen 80; server_name www.aaa.com; # www.aaa.com域名 location / { proxy_pass http://localhost:8080; # 对应端口号8080 } } server { listen 80; server_name www.bbb.com; # www.bbb.com域名 location / { proxy_pass http://localhost:8081; # 对应端口号8081 } }
在服务器8080和8081分别开了一个应用,客户端经过不一样的域名访问,根据server_name能够反向代理到对应的应用服务器。
虚拟主机的原理是经过HTTP请求头中的Host是否匹配server_name来实现的,有兴趣的同窗能够研究一下HTTP协议。
另外,server_name配置还能够过滤有人恶意将某些域名指向你的主机服务器。
用户(user):用户登陆后才能运行的程序,存在用户(user)。
/usr/lib/systemd/user
系统(system):如须要开机没有登录状况下就能运行的程序,存在系统服务(system)里。
/usr/lib/systemd/system
服务文件名以.service结尾:
vim /usr/lib/systemd/system/nginx.service
编写脚本内容(固定格式):
[Unit] Description=nginx After=network.target [Service] Type=forking
PIDFile=/usr/install/nginx/logs/nginx.pid ExecStart=/usr/install/nginx/sbin/nginx ExecReload=/usr/install/nginx/sbin/nginx -s reload ExecStop=/usr/install/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target
以上路径必须均为绝对路径!!而ExecStart、ExecReload、ExecStop的值也能够是"/etc/init.d"下的自定义的sh脚本文件的绝对路径,我就是采用这种方式来实现 uWSGI 开机启动的:
/etc/init.d目录下新建uWSGI服务启动脚本文件"uwsgi-start.sh": #!/bin/sh /pyvenv/bin/uwsgi --ini /pyvenv/src/eduonline/uwsgi.ini;
/etc/init.d目录下新建uWSGI服务重启脚本文件"uwsgi-restart.sh": #!/bin/sh /pyvenv/bin/uwsgi --restart /pyvenv/src/eduonline/uwsgi.pid;
/etc/init.d目录下新建uWSGI服务中止脚本文件"uwsgi-stop.sh": #!/bin/sh /pyvenv/bin/uwsgi --stop /pyvenv/src/eduonline/uwsgi.pid;
注意:sh脚本中一样采用绝对路径!!保存后,赋予可读可执行权限。而后编写service脚本文件。
systemctl enable nginx.service #".service"可省略
# 附其余命令:
systemctl start nginx.service # 启动
systemctl restart nginx.service # 重启,服务会停止一下子
systemctl reload nginx.service # 重载服务配置文件,相似于重启,但服务不会停止
systemctl stop nginx.service # 中止
systemctl disable nginx.service # 关闭开机启动
若是提示"Failed to execute operation: Access denied",输入"systemctl daemon-reexec"可解决。
首先,在Linux系统的"/etc/init.d/"目录下建立nginx脚本文件,使用以下命令:
touch nginx # 建立 vim nginx # 编辑
在脚本中添加以下命令:
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: NGINX is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" # 修改可执行文件路径 prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" # 修改配置文件路径 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -n "$user" ]; then if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done fi } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
保存脚本文件后对全部用户追加可执行权限:
chmod a+x /etc/init.d/nginx
先将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx
设置终端模式开机启动:
chkconfig nginx on
至此。转载请注明出处。