引言
emm,好久没有配置Nginx了,复习一下叭,经常不用真的会忘。
配置环境
这里就直接引用之前的文章中的内容了(fastDFS的那篇文章),因为这些都是常用的。
yum install git gcc gcc-c++ make cmake automake tree autoconf libtool curl wget pcre pcre-devel zlib zlib-devel openssl-devel wget unzip vim -y
特点、功能、用途
动静分离、反向代理、负载均衡
安装
# 从官网拉下来tar
# 解压
# cd 到nginx解压后的路径
cd /usr/local/nginx-1.20.1/
# 配置编译安装
./configure
make
make install
技术栈
配置
# 切换到安装后的路径
cd /usr/local/nginx/
# 切换到配置路径
cd conf
# 配置nginx服务器
vi nginx.conf
对于nginx.conf的解释
#user nobody; # 用户和用户组,这里通常是做访问权限的,没有特殊需求不需要修改
worker_processes 1; # 工作时的进程,通常时CPU数量或CPU数量*2
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 { # http服务器的配置
include mime.types; # 定义mime类型,类型是由mime.type文件定义的
default_type application/octet-stream; # 默认的类型,也就是如果没有定义类型的话,会在响应头上添加ContentType属性
#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; # 是否调用sendfile函数,是linux系统的一个函数,具体没有了解过,可以搜一下。
#tcp_nopush on; # 和sendfile有关,
#keepalive_timeout 0;
keepalive_timeout 65; # keepalive超时时间
#gzip on; # 是否启动gzip,一种压缩模块
server { # 服务配置
listen 80; # 监听端口
server_name localhost; # 服务名,可以是域名,也可以是IP
#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 # php结尾则自动转交给127.0.0.1的80端口
#
#location ~ \.php$ { # 匹配的后缀为PHP的
# proxy_pass http://127.0.0.1; # 代理路径
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # php脚本请求转发给FastCGI处理
#
#location ~ \.php$ {
# root html; # 根路径
# fastcgi_pass 127.0.0.1:9000; # fastcgi路径
# fastcgi_index index.php; # 默认页面
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # fastcgi脚本名称
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht { # 拒绝访问.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 # HTTPS
#
#server {
# listen 443 ssl; # https协议的监听端口
# server_name localhost; # 服务名称
# ssl_certificate cert.pem; # 证书路径
# ssl_certificate_key cert.key; # 私钥路径
# ssl_session_cache shared:SSL:1m; # ssl的会话缓存
# ssl_session_timeout 5m; # 会话超时时间
# ssl_ciphers HIGH:!aNULL:!MD5; # 私钥算法
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
动静分离
也就是动态页面仍然有Tomcat或者WebLogic进行驱动,而静态的页面以及静态资源,如HTML、CSS、JavaScript、图片等由Nginx处理,很大程度上能够减轻动态服务器的压力。配置的话,无非是端口号和资源路径。如果是https,需要申请SSL证书,配置pom和key文件路径,pom是证书、key是私钥。
负载均衡
负载均衡是一种动态策略,有轮询算法、权重(weight)、IP哈希、最少连接、响应时间等策略进行负载均衡。
- 轮询算法:每台机器一个一个的走,如果两个机器,第一个请求给1,第二个请求给2,第三个给1,第四个给2,轮询着分配。
- 权重,权重(weight=?)越低就分配的几率就越少
- IP哈希,根据IP的哈希值分配响应的服务器,保证同个IP由同一台服务器服务。
- 最少链接,那个服务器的连接少,就让谁服务,也要由权重跟着
- fair(响应时间),响应时间见解的能体现出服务器负载,响应快的服务。 需要fair模块
注意
这里的zunmx_blog是请求地址,修改代理地址,如下所示
轮询
upstream zunmx_blog{
server localhost:10000; # 第一台服务器
server localhost:10001; # 第二台服务器
}
权重
upstream zunmx_blog{
server localhost:10000 weight=3; # 第一台服务器
server localhost:10001 weight=7; # 第二台服务器
}
IPHash
upstream zunmx_blog{
ip_hash;
server localhost:10000 weight=3; # 第一台服务器
server localhost:10001 weight=7; # 第二台服务器
}
最少连接
upstream zunmx_blog{
least_conn;
server localhost:10000 weight=3; # 第一台服务器
server localhost:10001 weight=7; # 第二台服务器
}
fair
upstream zunmx_blog{
fair;
server localhost:10000 weight=3; # 第一台服务器
server localhost:10001 weight=7; # 第二台服务器
}
反向代理
相比正向代理,反向代理是客户端请求发送给前置服务器(nginx),前置服务器将请求转发给真正的服务器,这样隐藏了实际服务器的地址和端口。
配置
location / {
proxy_pass http://localhost:8080/myWeb/index.html;
}
文中用到的vim键位
vi 文件名 ==> 编辑那个文件
vim 快捷键
i 插入模式
命令
:q! 强退
:wq 写退

评论 0
还没有评论,来说点什么吧。