基本概念
nginx是什么?
高性能,http和反向代理的web服务器。
占有内存少,5000个并发。
反向代理?
正向代理:
用户不是直接访问服务器,而是通过浏览器配置一个代理服务器访问。
客户端无感知,不用配置代理,发送请求到代理服务器,由代理服务器转发请求。隐藏了真实的服务器ip地址。
负载均衡?
客户端多个请求到服务器,增加服务器的数量,将请求分发到不同服务器进行处理。
![](https://i-blog.csdnimg.cn/blog_migrate/dd6f8ea7d0ff844963bc360eb55209e5.png)
动静分离?
为了加快网站的解析速度,把动态页面和静态页面分开部署
高可用?
ninx安装:
nginx相关的依赖:
pcre-8.37.tar.gz 解压---
进入目录./configure ----执行i配置
make &&make install ----安装命令执行
./pcre-config --version ---查看是否成功
安装其他依赖:
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
安装nginx
tar -xvf nginx-1.12.2.tar.gz ---解压
进入目录./configure ----执行i配置
make &&make install ----安装命令执行
安装失败:
src/os/unix/ngx_user.c: In function ‘ngx_libc_crypt’:
src/os/unix/ngx_user.c:36:7: error: ‘struct crypt_data’ has no member named ‘current_salt’
由于我的系统是centos8,导致报错
通过yum安装:https://www.cnblogs.com/opsprobe/p/10773582.html
防火墙:
systemctl start firewalld.service ##启动
查看开放的端口号 firewall-cmd --list-all
设置开放的端口号 firewall-cmd --add-service=http –permanent
sudo firewall-cmd --add-port=80/tcp --permanent
重启防火墙 firewall-cmd –reload
nginx配置文件目录:
/etc/nginx/nginx.conf
常用命令
先进入sbin:/usr/sbin
查看nginx版本
./nginx -v
启动nginx
./nginx
关闭nginx
./nginx -s stop
重新加载nginx
./nginx -s reload
配置文件
nginx.conf 包含三大块
全局块
配置文件开始到events
events块
http块
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
##用于配置负载均衡
upstream myserver {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
listen 80 ;
server_name 123.56.1.19;
index index.html index.htm;
##root html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://myserver;
}
##静态资源配置
location /www/ {
root /home/temp/; ##访问的根路径
index index.html index.html;
}
location /images/ {
root /home/temp/; ##访问的根路径
autoindex on; ##
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 7002;
server_name 123.56.1.19;
###根据请求的url转发请求,正则表达式
location ~ /gov/ {
##代理的服务地址
proxy_pass http://127.0.0.1:8080;
}
location ~ /edu/ {
proxy_pass http://127.0.0.1:8081;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
配置nginx
反向代理
实现效果:打开浏览器之后,通过nginx代理到tomcat应用
配置文件:
docker
重启docker解决;
docker: Error response from daemon: driver failed programming external connectivity on endpoint nginx (69dcee3405a30059b7ae85116574b341c65978e4e6ce9b1d563e305af8271884): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 80 -j DNAT --to-destination 172.18.0.4:80 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).
负载均衡
有几种方式:
轮循,权重,ip_hash,fair(第三方)
配置:
nginx 分配服务器策略
第一种 轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
第二种 weight weight 代表权重默认为 1,权重越高被分配的客户端越多
第三种 ip_hash 每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器
第四种 fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。
动静分离
静态资源存放在本机 添加如下配置:
##静态资源配置
location /www/ {
root /home/temp/; ##访问的根路径
index index.html index.html;
}
location /images/ {
root /home/temp/; ##访问的根路径
autoindex on; ##
}
/home/temp/www/ 通过此url就能实现访问 http://123.56.1.19/www/a.html
/home/temp/images/ 通过 http://123.56.1.19/images/psb.jpg 访问到图片