// 安装依赖
cd /usr/local/src
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel //安装准备:nginx依赖于pcre、zlib、ssl库
要先安装pcre、zlib、ssl
// 下载
cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -zxvf nginx-1.13.7.tar.gz
// 安装
cd nginx-1.13.7
./configure
make && make install
// 环境变量
vim /etc/profile
PATH=$PATH:/usr/local/nginx/sbin
export PATH
:wqa
// 刷新
source /etc/profile
相关命令
// 启动
./nginx
// 立刻停止 Nginx 服务
nginx -s stop
// 平滑停止 Nginx 服务
nginx -s quit
// 重新加载配置文件
nginx -s reload
// 重新打开日志=
nginx -s reopen
// 指定配置文件
nginx -t conf
// 测试配置文件是否正确(语法检测)
nginx -t
// 显示 Nginx 版本信息
nginx -v
// 显示 Nginx 版本信息、编译器和配置参数的信息
nginx -V
文件一揽
配置文件预览 全局块 main: 从配置文件开始到events块之间的内容,主要配置影响nginx服务整体运行的配置指令,主要包括配置运行nginx服务器的用户组、允许生成 worker process 数、进程PID存放路径、日志存放路径和类型以及配置文件的引入等
nobody 表示任何人都可用
user nobody;
worker_processes 1;
work_cpu_afinity 0001;
# error_log logs/error.log; // notice info warn
#pid logs/nginx.pid;
events: 影响nginx服务器与客户端之间的网络连接
events {
# woker最大连接数
worker_connections 1024;
# 网络连接接收序列化
# accept_mutex on;
# 是否重复接收连接
# multi_accept off;
# 获取mutex的周期
accept_mutex_delay 500ms;
# 指定IO模型
# use kqueue | 看平台 linix 2.6 select poll yishang epoll kqueue sigio
# 连接保活时间
keepalive_timeout 120;
# 开启压缩
gzip on;
}
http: 包含http全局块和sever块
http {
# 引入配置
include mime.types;
# 指定默认类型
default_type application/octet-stream;
# sendfile相关
# sendfile on;
# sendfile_max_chunk 128K;
# 会话相关
keepalive_timeout 120;
# keepalive_requests number;
# 客户端请求头相关
client_header_buffer_size 32k;
client_max_body_size 8m;
# cgi相关
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_min_length 1000;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
# 监听端口
listen 8000;
# 服务名称
server_name localhost; // ip、domain
# 地址定向、数据缓存、应答控制
location / {
root html;
index index.html index.htm;
}
# 错误页
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
概念: 动态资源(jsp、ftl、thymeleaf)与静态资源(js、css、img)分开部署。将静态资源部署在Nginx上,当一个请求来的时候,如果是静态资源的请求,就直接到nginx配置的静态资源目录下面获取资源,如果是动态资源的请求,nginx利用反向代理的原理,把请求转发给后台应用去处理,从而实现动静分离。
# 静态转发
server {
root /home/www;
# \ 对目录的请求
location ~ .*\.(js|css|jpg|jpeg|gif|png|ico|pdf|txt)$ {
# root /home/static;
# proxy_pass http://192.168.162.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding "";
sub_filter_types *;
sub_filter_once off;
sub_filter 'indexTips' 'indexTips1';
}
}
概念: 客户端对反向代理服务器是无感知的,无需任何配置就可以访问,客户端只需要将请求发送到反向代理服务器,由反向代理服务器选择目标服务器,此时反向代理服务器和目标服务器就是一个服务器,暴露的是反向代理服务器的地址,隐藏了真实地址。 配置示例
server
{
sever_name locahost;#本地服务
listen 80;#监听端口
location ~ /a {
proxy_pass http://192.168.33.99:801;#http://不可省略
}
location ~ /b {
proxy_pass http://192.168.33.98:802/;#http://不可省略
}
}
http{
# 轮询
upstream backend_default {
server 192.168.0.1 down;
server 192.168.0.2 ;
}
# 权重
upstream backend_weight {
server 192.168.0.1 weight = 1 ;
server 192.168.0.2 weight =2 ;
}
# IP HASH
upstream backend_default {
ip_hash;
server 192.168.0.1 backup;
server 192.168.0.2;
}
# FAIR
upstream backend_default {
server 192.168.0.1;
server 192.168.0.2;
fair;
}
# ...
server
{
listen 80;#监听端口
# 域名
sever_name www.wangxu.com wangxu.cn;
location ~ /a {
proxy_pass http:backend_xxx;#http://不可省略
}
}
}
日志管理
日志分类
xxx_log path [format [buffer=size[flush=time]]];
# 示例
access_log log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
// 安装
yum install logrotate
// 每周切割一次日志
/var/log/nginx/*.log {
weekly | daily | monthly
missingok
rotate 52
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
[ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
endscript
}
gzip压缩
http {
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}
IO模型
- select: linux最原始IO复用,最大优势 移植性好
- 数据结构: 数组 ulitmit -a open files file descp
- 在用户态遍历数组,效率低下
// man select 2
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
void FD_CLR(int fd, fd_set *set);
int FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);
- poll: 针对select改进,优势比select
- 数据结构:链表,ulimit -a |
- 改进方式:把数据交换从用户态提升内核态
// man poll
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <poll.h>
// 数据定义
struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};
master:独一无二、读取配置、校验语法、平滑处理、管理worker worker:社畜一号
安装报错
- QAQ :
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
- ASW :
// 使用nginx -c的参数指定nginx.conf文件的位置
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
如果您喜欢我的文章,请点击下面按钮随意打赏,您的支持是我最大的动力。
最新评论