1、nginx基本概念
(1)nginx是什么,做什么事情?
nginx(“engine x”)是一个高性能的HTTP和反向代理服务器,特点是占用内存小,并发能力强。nginx专为性能优化而开发,能够经受高负载的考验。
(2)反向代理
要理解,反向代理首先理解正向代理,所谓正向代理就是在客户端配置代理服务器,通过代理服务器访问网址。
反向代理,客户端无需任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器选择目标服务器获取数据后,再返回给客户端,
此时反向代理服务器和目标服务器对外就是一个服务器,暴露的就是代理服务器的地址,隐藏了真实服务器的IP地址。
(3)负载均衡
随着当访问量和数据量飞速增长,传统的客户端访问服务器的流程已经不再使用。当并发量特别大时会造成服务器宕机。
这个时候集群的概念就产生了,我们可以增加服务器数量,然后将请求分发给各个服务器,将原先请求集中到单个服务器的情况改为分发到多个服务器上,
将负载分发到不同的服务器也就是我们所说的负载均衡。
(4)动静分离
为了加快网站的解析速度,可以把静态资源和动态资源请求分发到不同的服务器,加快解析速度,降低原来单个服务器的压力。
2、nginx的安装、常用命令和配置文件
(1)在Linux系统中安装nginx
2.1安装pcre依赖
https://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz/download 下载pcre-8.37.tar.gz,
http://nginx.org/download/nginx-1.12.2.tar.gz下载nginx-1.12.2.tar.gz并上传到服务器
- 解压
tar -xvf pcre-8.37.tar.gz
- 进入解压后的目录,执行
./configure
- 使用
make && make install
,这里遇到一个问题,configure: error: You need a C++ compiler for C++ support. - 先安装c++编译器
yum install -y gcc gcc-c++
,重新./configure
,没问题后 - 使用
make && make install
安装 pcre-config --version
查看版本号
2.2安装其他的依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2.3安装nginx
-
解压
tar -xvf nginx-1.12.2.tar.gz
-
进入解压后的目录,执行
./configure
-
make && make install
-
安装成功后 在usr多出一个文件夹/local/nginx,里面有一个sbin的启动脚本
-
进入
cd /usr/local/nginx/sbin
*./nginx
启动nginx -
进入conf目录,这里有一个nginx.conf文件
可以通过localhost 即服务器地址,访问
-
查看开放端口号
firewall-cmd --list-all
-
设置开放端口号
sudo firewall-cmd --add-service=http -permanent
-
查看防火墙 状态
systemctl status firewalld
-
开启防火墙
systemctl start firewalld
-
关闭防火墙
systemctl stop firewalld
-
重加载防火墙
firewall-cmd --reload
(2)nginx常用命令
需要进入sbin目录才行
- 查看nginx版本号
./nginx -version
- 关闭nginx
./nginx -s stop
- 启动nginx
./nginx
- 重加载nginx,比如改了nginx.conf 无需重启
./nginx -s reload
2、nginx配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 {
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;
# }
#}
}
主要由三部分组成
全局块
从配置文件到events块之间的内容,主要设置一些影响nginx服务器整体运行的配置指令
events块
涉及的指令主要影响Nginx服务器与用户的网络链接
http块
最频繁的配置部分,包括http全局块、server块
3、nginx配置实例1-反向代理
实现效果
浏览器访问www.baidu.com,跳转到linux系统中的tomcat主页面。
访问过程分析
- 修改nginx.conf文件
- 效果演示
使用以下网址
http://www.baidu.com
会跳转到服务器的8080端口即
http://127.0.0.1:80808
实现效果
访问http://127.0.0.1:9001/edu/ 直接跳转到127.0.0.1:8080
访问http://127.0.0.1:9001/org/ 直接跳转到127.0.0.1:8081
准备两个tomcat服务器,修改一下第二个tomcat的配置文件server.xml
访问过程分析
server {
listen 9001;
server_name localhost;
location ~ /edu/ {
proxy_pass http://localhost:8080;
}
location ~ /org/ {
proxy_pass http://localhost:8081;
}
}
4、nginx配置实例2-负载均衡
#加上负载均衡服务列表
upstream myserver{
server localhost:8080
server localhost:8081
}
server {
listen 80;
server_name localhost;
location ~ /edu/ {
proxy_pass http://myserver;
root html;
index index.html index.htm;
}
}
nginx服务器分配策略
- 1、轮询,每个服务器按时间顺序逐一分配到不同的服务器
- 2、权重
- 3、ip_hash 根据访问者ip分配,
- 4、fair 按后端服务器响应时间分配,谁的响应时间短给谁分配
5、nginx配置实例3-动静分离
server {
listen 80;
server_name localhost;
location ~ /www/ {
root /data/;
index index.html index.htm;
}
location ~ /image/ {
root /data/;
autoindex on;
}
}
6、nginx配置实例4-高可用集群
- 准备工作,需要在两个服务器上都装上nginx,作为主从nginx服务器;在两台服务器上都装上keepalived
- yum install keepalived -y
- 安装完成后,在etc里生成目录keepalived 有配置文件keepalived.conf
7、nginx原理