安装
setenforce 0systemctl stop firewalld.service安装nginx依赖的库yum -y install gccyum -y install gcc-c++yum -y install pcre pcre-devel yum -y install zlib zlib-develyum install -y openssl openssl-devel 这三个需要超级管理员权限进行安装 su进入超级管理员模式输入密码wget http://nginx.org/download/nginx-1.9.0.tar.gz 下载tar -zxvf nginx-1.9.0.tar.gz 解压cd nginx-1.9.0#./configure --with-http_ssl_module 配置#--prefix指定安装目录#--with-http_ssl_module安装https模块./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_modulemake && make install编译 安装 whereis nginx 查看安装位置sbin/nginx -s stop 停止sbin/nginx -s reload 重启sbin/nginx -t 检查Nginx是否有异常sbin/nginx -c /usr/local/nginx/conf/nginx.conf 使用那个配置文件进行启动
Nginx配置
分为3部分:全局块 events块 http块
全局块
#全局块
全局块作用:全局指令 指定运行时的用户组 进程id存放位置 日志存放位置 worker process数量
#user nobody; 配置用户或者用户组
worker_processes 1; #允许生成的进行数
#日志路径的设置 可以配置日志级别 debug >info>notice>warn >error >crit
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;存储进程id
events块
#events块
影响Nginx服务器和用户网络连接的配置 比如每个进程最大连接数、选取那种事务驱动模型、在网络连接过程中是否开启多个、是否序列化
events {
#每个进程的最大连接数
worker_connections 1024;
}
http块
#http块
配置代理、缓存、日志等绝大部分功能的地方 可以嵌套多个server块 不同的server可以对应多个域名
http {
include mime.types;
default_type application/octet-stream;
#日志配置 可以获取用户地址信息 跳转来源 终端 以及url访问量
#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; main表示日志生成的格式 格式在log_format设置的
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; #相对路径的配置 nginx文件下的目录
index index.html index.htm;#跳转的首页地址
}
}
location块的匹配规则
匹配规则
location=/url/ 精准匹配
location ^~/url 前缀匹配 location /url 前缀匹配 在正则匹配之后
location规则 正则匹配 区分大小写
location ~*规则 正则匹配 不区分大小写
location / 通用匹配 接收未匹配到其他location的请求