nginx基础
1、基本操作
1.1 yum 安装
# 查看nginx安装信息
[root@VM-4-3-centos ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
# 常用模块
Nginx编译选项 模块作⽤
ngx_http_core_module 包含⼀些核⼼的http参数配置,对应Nginx的配置区块部分
ngx_http_access_module 访问控制模块,⽤来控制⽹站⽤户对Nginx的访问
ngx_http_gzip_module 压缩模块,对Nginx返回的数据压缩,属于性能优化模块
ngx_http_fastcgi_module fastci模块,和动态应⽤相关的模块,例如PHP
ngx_http_proxy_module proxy代理模块
ngx_http_upstream_module 负载均衡模块,可以实现⽹站的负载均衡功能及节点的健康检查。
ngx_http_rewrite_module URL地址重写模块
ngx_http_limit_conn_module 限制⽤户并发连接数及请求数模块
ngx_http_limit_req_module 限制Nginx request processing rate根据定义的key
ngx_http_log_module 访问⽇志模块,以指定的格式记录Nginx客户访问⽇志等信息
ngx_http_auth_basic_module Web认证模块,设置Web⽤户通过账号密码访问Nginx
nginx_http_ssl_module ssl模块,⽤于加密的http连接,如https
# Nginx内置变量
http核⼼模块的内置变量
http请求变量
Nginx内置变量
⾃定义变量
$uri: 当前请求的uri,不带参数 ⽐如访问:xx.com/url?name=wing 获取到 xx.com/url
$request_uri: 请求的uri,带完整参数 ⽐如访问:xx.com/url?name=wing 获取到 xx.com/url?
name=wing
$host: http请求报⽂中host⾸部,如果没有则以处理此请求的虚拟主机的主机名代替
$hostname: nginx服务运⾏在主机的主机名
$remote_addr: 客户端IP
$remote_port: 客户端端⼝
$remote_user: 使⽤⽤户认证时客户端⽤户输⼊的⽤户名
$request_filename: ⽤户请求中的URI经过本地root或alias转换后映射的本地⽂件路径
$request_method: 请求⽅法, GET POST PUT DELET
$server_addr: 服务器地址
$server_name: 服务器名称
$server_port: 服务器端⼝
$server_protocol: 服务器向客户端发送响应时的协议, 如http/1.1 http/1.0
$scheme:在请求中使⽤scheme, 如http://xxx.com中的http
$http_HEADER: 匹配请求报⽂中指定的HEADER
$http_host: 匹配请求报⽂中的host⾸部
1.2 nginx基本配置
# 配置带域名的虚拟主机(一个网站可以有多个域名)
[root@VM-4-3-centos nginx]# egrep -v "^$|.*#" nginx.conf.bak
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
[root@VM-4-3-centos nginx]# egrep -v "^$|.*#" nginx.conf.bak > nginx.conf
[root@LNMP conf]# cat conf.d/{www,bbs}.conf
server {
listen 80;
server_name www.wingsredevsecops.top;
root /soft/code/www;
}
server {
listen 80;
server_name bbs.wingsredevsecops.top;
root /soft/code/bbs;
}
1.3 配置多端口虚拟主机
# 配置nginx
[root@VM-4-3-centos conf.d]# cat 800*
server {
listen 8001;
root /soft/code/8001;
index index.html index.htm;
}
server {
listen 8002;
root /soft/code/8002;
index index.html index.htm;
}
1.4 添加日志
[root@VM-4-3-centos conf.d]# cat bbs.conf
server {
listen 80;
server_name bbs.wingsredevsecops.top;
root /soft/code/bbs;
location /nginx_status{
stub_status on;
access_log off;
}
}
1.45 下载目录
//开启⽬录浏览
location /download {
root /soft/code/wing;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
}
//autoindex常⽤参数
autoindex_exact_size off;
默认为on, 显示出⽂件的确切⼤⼩,单位是bytes。
修改为off,显示出⽂件的⼤概⼤⼩,单位是kB或者MB或者GB。
autoindex_localtime on;
默认为off,显示的⽂件时间为GMT时间。
修改为on, 显示的⽂件时间为⽂件的服务器时间。
charset utf-8,gbk;
默认中⽂⽬录乱码,添加上解决乱码。
1.46 静态资源跨域访问
# 添加请求头,可以成功跨域访问,ajax应该比较多
server {
listen 80;
server_name wing.com;
root /soft/code/wing.com/;
index index.html index.htm;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials: true;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,UserAgent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
1.47 静态资源防盗链
//⽀持IP、域名、正则⽅式
location ~ .*\.(jpg|gif|png)$ {
valid_referers none blocked static.wingsredevsecops.top;
if ($invalid_referer) {
return 403;
}
root /soft/code/images;
}