nginx是一个高性能的Web服务器,通常我们会使用nainx来做反向代理处理和静态资源请求。它在处理静态资源方面,吞吐量比tomcat高很多。
一、Linux安装
1.环境安装
1.1 安装gcc、g++
# 依赖rpm包
cpp-4.8.5-28.el7.x86_64.rpm
gcc-4.8.5-28.el7.x86_64.rpm
gcc-c++-4.8.5-36.el7.x86_64.rpm
glibc-devel-2.17-222.el7.x86_64.rpm
glibc-headers-2.17-222.el7.x86_64.rpm
kernel-headers-3.10.0-862.el7.x86_64.rpm
libmpc-1.0.1-3.el7.x86_64.rpm
mpfr-3.1.1-4.el7.x86_64.rpm
# 安装指令
rpm -ivh *.rpm --nodeps
# 检查版本
gcc -v
g++ -v
1.2 安装pcre
pcre-8.32-17.el7.x86_64.rpm
pcre-devel-8.32-17.el7.x86_64.rpm
# 安装指令
rpm -ivh pcre-8.32-17.el7.x86_64.rpm
rpm -ivh pcre-devel-8.32-17.el7.x86_64.rpm
1.3 安装zlib
zlib-1.2.7-18.el7.x86_64.rpm
zlib-devel-1.2.7-18.el7.x86_64.rpm
#安装指令
rpm -ivh zlib-1.2.7-18.el7.x86_64.rpm
rpm -ivh zlib-devel-1.2.7-18.el7.x86_64.rpm
2.安装nginx
2.1 安装
# 解压nginx的tar包
tar -xzvf nginx-1.16.1.tar.gz
# 进到解压目录下
cd /nginx
# 生成 makefile 文件
./configure
# 编译
make
# 安装
make install
2.2 验证
nginx的默认安装路径为:/usr/local/nginx
# 添加nginx用户
useradd nginx -s /sbin/nologin -M
id nginx
# 启动nginx
/usr/local/nginx/sbin/nginx
# 测试
/usr/local/nginx/sbin/nginx -t
# 启动之后访问服务器本地地址的80端口能看到nginx的页面
二、nginx启动、停止与重启及常用命令
1.启动
# 配默认配置启动
/usr/local/nginx/sbin/nginx
# 配置文件启动
/usr/local/nginx/sbin/nginx -c /nginx/nginx.conf
2.停止
3种停止方式
# 从容停止nginx 进程完成当前工作后再停止
/usr/local/nginx/sbin/nginx -s quit
# 立即停止服务无论进程是否在工作,都直接停止进程
/usr/local/nginx/sbin/nginx -s stop
# kill 方法杀死进程
# 查看nginx进程
ps -ef|grep nginx
# 进程信息
UID PID PPID C STIME TTY TIME CMD
root 3274 1 0 2019 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 7609 3274 0 14:59 ? 00:00:02 nginx: worker process
root 17157 4175 0 17:11 pts/1 00:00:00 grep --color=auto nginx
# 暴力杀死 PID 进程
kill -9 3274
kill -9 7609
3.重启nginx
# 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reload
4.常用命令
# 查看帮助信息
nginx -h
# 查看nginx版本(小写字母v)
nginx -v
# 除版本信息外还显示配置参数信息(大写字母V)
nginx -V
# 指定配置文件启动nginx
nginx -c filename
# 关闭nginx,完整有序的停止nginx,保存相关信息
nginx -s quit
# 关闭nginx,快速停止nginx,可能并不保存相关信息
nginx -s stop
# 重新载入nginx,当配置信息修改需要重新加载配置是使用
nginx -s reload
# 重新打开日志文件
nginx -s reopen
# 测试nginx配置文件是否正确
nginx -t -c filename
三、反向代理
1.正向代理和反向代理的区别
正向代理就是客户端(用户)经过代理商(代理服务器)然后才能访问到目标服务器的服务。目标服务器它并不知道真正的请求是从哪里来的。
比如要访问twitter,那么我们就需要一个能够访问到twitter的服务器,这个服务器就是代理服务器。用户想要使用twitter的服务,但是不知道怎样才能直接访问,而这时代理服务器就好比twitter暴露出来的一个访问接口(中间人)。
反向代理则是将服务端(服务)代理给代理商(代理服务器),然后服务端对外说需要我们的服务去找代理商就可以了。客户端(用户)并不知道处理请求的真正的服务器是在哪里。
比如淘宝网:淘宝网提供了许多的服务,如主页,搜索,用户,下单等。其实这些服务可能都被部署到不同地方的服务器上,如主页服务的服务器在上海、搜索的在北京、用户的在深圳、下单的在杭州。但是我们只是通过淘宝的ip地址就能访问到所有的淘宝服务,这其中起作用的就是反向代理。当然阿里帝国是很强大的,反向代理只是阿里解决问题的一个方法,以上都是我瞎猜的。
正向代理和反向代理的区别在于代理的对象不一样,正向代理的代理对象是客户端,反向代理的代理对象是服务端。
这是我在知乎上找到的一张图可以帮助我们很好的理解:
2.nginx做反向代理
1.修改nginx.conf配置文件
location / {
proxy_pass http://127.0.0.1:8111;
}
location / 表示处理所有请求
proxy_pass http://127.0.0.1:8111; 表示把请求都交给http://127.0.0.1:8111来处理
ip地址和端口可以根据实际情况来修改
修改完配置文件必须通过 nginx -s reload 来重新载入
四、动静分离
前面提到的,因为nginx在处理静态文件的吞吐量上面比tomcat好很多,所以通常将他们俩配合使用。不会把所有的请求都交给tomcat处理, 而是把静态请求交给nginx,动态请求交给tomcat。 从而达到动静分离的效果。
nginx做动静分离
配置文件server里面添加
location ~\.(css|js|png)$ {
root /test/nginx;
}
这表示所有的css js png访问都由nginx来做,访问的地址是root后面的路径
负载均衡
当服务访问量很大的时候,一个 Tomcat 吃不消了,这时候就准备多个 Tomcat,由Nginx按照权重来对请求进行分配,从而缓解单独一个Tomcat受到的压力。
增加一个upstream ,用来指向这两个tomcat
upstream test{
server 127.0.0.1:8111 weight=1;
server 127.0.0.1:8222 weight=2;
}
location / {
proxy_pass http://test;
}
weight表示权重,最大值是10,越高分配到的几率就越大。
五、配置文件
默认配置文件nginx.conf
user nginx nginx; #定义Nginx运行的用户和用户组
worker_processes 1; #nginx进程数,建议设置为等于CPU总核心数。
error_log /var/log/nginx/error.log info; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
pid /var/run/nginx.pid; #进程文件
worker_rlimit_nofile 1024; #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以#建议与ulimit -n的值保持一致
events
{
use epoll; #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,
如果跑在FreeBS #D上面,就用kqueue模型。
worker_connections 65535; #单个进程最大连接数(最大连接数=连接数*进程数)
}
http #HTTP区块开始
{
include mime.types; #Nginx支持的媒体类型库文件
default_type application/octet-stream; #默认媒体类型
#charset utf-8; #默认编码
server_names_hash_bucket_size 128; #服务器名字的hash表大小
client_header_buffer_size 32k; #上传文件大小限制
large_client_header_buffers 4 64k; #设定请求缓
client_max_body_size 8m; #设定请求缓
sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为o #ff,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #防止网络阻塞
keepalive_timeout 120; #连接超时,单位是秒
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/x-javascript text/css application/xml; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用
#虚拟主机的配置
server
{
listen 80; #监听端口
server_name localhost; #提供服务的域名主机名
location / { #第一个location区块开始
root html; #站点的根目录,相当于Nginx的安装目录
index index.html index.htm index.jsp; #默认的首页文件,多个用空格分开
} #第一个location区块结果
#图片缓存时间设置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS和CSS缓存时间设置
location ~ .*\.(js|css)?$
{
expires 1h;
}
#日志格式设定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/log/nginx/access_$(data+%F -d -1day).log access; #定义本虚拟主机的访问日志
location / { #对 "/" 启用反向代理
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可选
proxy_set_header Host $host;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd; #htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}
#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}
nginx.conf引用default.conf
nginx.conf文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#gzip on;
#gzip on;
proxy_buffer_size 256k;
proxy_buffers 32 256k;
proxy_busy_buffers_size 256k;
gzip on;
#gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 10 16k;
#gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png video/mp4;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
#这个参数表示http连接超时时间,默认是65s。要是上传文件比较大,在规定时间内没有上传完成,就会自动断开连接!所以适当调大这个时间。
keepalive_timeout 6000;
fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
fastcgi_read_timeout 6000;
fastcgi_buffer_size 512k;
fastcgi_buffers 8 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
##
client_header_timeout 6000; #调大点
client_body_timeout 6000; #调大点
client_max_body_size 500m; #主要是这个参数,限制了上传文件大大小
client_body_buffer_size 512k;
proxy_redirect off;
proxy_next_upstream off;
proxy_connect_timeout 18000; ##修改成半个小时
proxy_send_timeout 18000;
proxy_read_timeout 18000;
include /etc/nginx/conf.d/*.conf;
}
default.conf文件
路径为nginx.conf文件倒数第2行的 /etc/nginx/conf.d/
server {
listen 80;
server_name localhost;
client_max_body_size 500M;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#slice 1m;
# proxy_cache_key $host$uri$is_args$args$slice_range;
# proxy_set_header Range $slice_range;
proxy_http_version 1.1;
proxy_cache_valid 200 206 1h;
proxy_cache_lock on;
proxy_cache_lock_timeout 0s;
#proxy_cache_lock_age 200s;
proxy_cache_use_stale updating;
location / {
# root html;
# index index.html index.htm;
alias /***/;
}
## Prohibit search engine search
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
{
return 403;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/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;
#}
}