Nginx简介
Nginx作为web服务器的优点:
Nginx基本功能:
nginx的安装
下载源码包
wget http://nginx.org/download/nginx-1.17.5.tar.gz
#或通过浏览器访问官网http://nginx.org/en/download.html进行下载
#若使用最小化安装方式可使用yum install wget命令安装wget工具
#wget默认下载到当前目录下,使用-O指定下载文件路径及名称
安装依赖及源码安装所需包
创建nginx运行所使用的系统用户
解压缩
编译安装
cd nginx-1.17.5
./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/aaccess.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
#--prefix=/usr/local/nginx 指定程序安装目录
#--conf-path=/etc/nginx/nginx.conf 指定配置文件路径
#--error-log-path=/var/log/nginx/error.log 错误日志存放路径
#--http-log-path=/var/log/nginx/aaccess.log 访问日志存放路径
#--pid-path=/var/run/nginx.pid 指定pid文件
#--lock-path=/var/lock/nginx.lock 指定lock文件
#--user=nginx --group=nginx 指定程序所属主和组
#--with-http_ssl_module 启用ssl模块
#--with-http_v2_module 启用http2模块
#--with-http_dav_module 启用文件自动化管理模块
#--with-http_stub_status_module 启用网页状态检测模块
# --with-threads 启用线程池
#--with-file-aio 启用异步I/O
#更多参数参考官方文档:http://nginx.org/en/docs/configure.html
#更多安装过程详情参考官方文档:https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/
修改环境变量
修改配置文件
user nginx nginx; #指定属主和属组
worker_processes 4; #启用进程数建议<=CPU核心数
worker_cpu_affinity 0001 0010 0100 1000; #将进程绑定到指定的cpu核心上
#也可以使用八位二进制表表示,1表示使用该位置上的cpu核心,从右向左数,第几位表示第几个核心
worker_rlimit_nofile 65535; #单个进程能够打开的最大文件句柄数,建议与系统设定保持一致
work_priority -10; #调整进程的nice值,root调整范围[-20,19]普通用户[100,139],越小越优先
#time_resolution #计时器解析值,减少gettimeofday()系统调用的次数,提升性能
#error_log logs/error.log; #指定错误日志路径和级别
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events { #进程相关配置
use epoll; #指定进程所使用的事件模型
#包括epoll、select、poll、rtsig
worker_connections 10240; #每个进程最大响应连接数
}
使用nginx -t检查配置文件语法格式
启动服务
#常用操作命令
nginx #启动服务
nginx -s SINGLE #发送指定信号
#常用信号:
# reload 重载服务
# stop 停止
# quit 退出
# reopen 重启
配置防火墙规则
浏览器访问测试
基于端口的虚拟主机配置
修改主配置文件
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #定义日志格式,使用main必须开启
# '$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 { #类似virtualhost
listen 80; #监听端口
#也可以监听指定ip地址的某个端口 Listen 192.168.10.124:8000
server_name web1.lg.com; #服务名
# server_name可以指定一个或多个,名称可以使用正则表达式(~)或通配符
#匹配规则:高精度优先
charset utf-8; #使用字符集
access_log logs/host.access.log main; #访问日志相对路径,
location / { #资源路径映射
root /var/www/html/web1; #网页所在路径(相对或绝对路径)
index index.html index.htm; #主页类型
}
#location [=|~|~*|^~|@]
#= 表示精确匹配
#^~ 匹配URI的前半部分,不支持正则表达式
#~ 正则表达式
#~* 不区分大小写的正则表达式
#匹配优先级 = ^~ ~ ~*
#location中还可以使用alias定义别名
error_page 404 /404.html; #404页面,相对路径,指定目录下
error_page 500 502 503 504 /50x.html; #50X页面
location = /50x.html {
root html;
}
}
server {
listen 8080;
server_name web2.lg.com;
charset utf-8;
# listen somename:8080;
# server_name somename alias another.alias;
location / {
root /var/www/html/web2;
index index.html index.htm;
}
}
}
#相对路径相对于程序安装路径,这里是/usr/local/nginx/目录下
创建指定的目录及文件
重载服务
客户机访问测试
匹配配置
修改配置如下:
分别在指定的目录下创建对应文件夹及文件
重载服务
测试
基于域名的虚拟主机配置
修改配置文件
重载服务
修改客户机C:\Windows\System32\drivers\etc\hosts文件,添加ip和域名的映射关系
访问测试
Nginx访问控制
修改配置文件如下
location / {
root /var/www/html/web1;
index index.html index.htm;
allow 192.168.10.0/24; #指定允许访问的ip或网段
deny all; #拒绝的ip或网络,all表示所有,即默认拒绝所有
}
或使用用户访问控制
创建指定的目录及文件
重载服务
测试
搭建https
搭建CA服务器
CA服务器生成私钥
创建证书编号文件并写入序列编号
进行自签
nginx服务端生成私钥
生成签证请求
将请求发送至CA服务器进行签证
CA服务器进行签证
将签证证书发回nginx服务器
将nginx服务器的私钥和证书存放至指定路径
修改配置文件
server {
listen 443 ssl;
server_name www.lg.com;
charset utf-8;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.private_key;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html/www;
index index.html index.htm;
}
}
创建测试页面
重载服务
测试
启用stat服务状态页
修改配置文件
重载服务
访问状态页
Active connections: 1 #活动连接数量
server accepts handled requests
94 94 452 #分别为接受的连接 处理过的连接 处理的请求
Reading: 0 Writing: 1 Waiting: 0
#分别为 正在接受的请求 完成的请求,处于发送响应报文状态 等待的活动连接请求
URL重写
修改配置文件定义重写规则
server {
listen 80;
server_name web2.lg.com;
charset utf-8;
# listen somename:8080;
# server_name somename alias another.alias;
location / {
root /var/www/html/web2;
index index.html index.htm;
rewrite ^/img/(.*)$ /images/$1 break
#定义转发规则,将访问img的请求转到images/目录下
#break 表示重写结束跳出循环
#还可以使用
#last 重写后不被其他规则处理
#redirect 以302状态响应码返回新URL,属于临时重定向
#以301状态响应码返回新URL,属于永久重定向
}
}
创建对应目录及文件
重载服务
测试
if上下文
通常定义在location或server上下文中
语法:if (condition) {......}
使用容器:server,location
组成:
测试指定目录是否存在 -d !-d
修改配置文件如下
server {
listen 8080;
server_name web2.lg.com;
charset utf-8;
location / {
root /var/www/html/web2;
index index.html index.htm;
if(!-e $document_root/img/index.html){ #如果不存在/img/index.html则执行重写
rewrite ^/img/(.*)$ /images/$1 break;
}
}
}
重载配置
测试
为img目录添加index.html文件
访问测试
判断条件成立,不执行重定向
原文:https://www.cnblogs.com/lastyear/p/11838912.html