Nginx 入门配置
编译安装
安装依赖
需要 ROOT 权限和可访问的 YUM 源1
yum install gcc gcc-c++ openssl openssl-devel pcre pcre-devel zlib zlib-devel patch make
编译安装
解压相应的 tar.gz 二进制包(ngx_http_proxy_connect_module 是正向代理访问 https 所需模块),然后将模块放在固定的位置,进入 nginx-1.20.11
2
3
4
5mkdir -pv /app/service/nginx/modules && mkdir -pv /app/logs/nginx
tar -zxvf nginx-1.20.1.tar.gz && tar -zxvf ngx_http_proxy_connect_module-0.0.2.tar.gz
mv ngx_http_proxy_connect_module-0.0.2 /app/service/nginx/modules/ngx_http_proxy_connect
cd nginx-1.20.1
patch -p1 < /app/service/nginx/modules/ngx_http_proxy_connect/patch/proxy_connect_rewrite_1018.patch
打完补丁以后,进行编译安装1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32./configure \
--prefix=/app/service/nginx \
--conf-path=/app/conf/nginx/nginx.conf \
--error-log-path=/app/logs/nginx/error.log \
--http-log-path=/app/logs/nginx/access.log \
--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 \
--add-module=/app/service/nginx/modules/ngx_http_proxy_connect
#--add-dynamic-module=/app/service/nginx/modules/ngx_http_proxy_connect
make && make install
安装完成以后,上面已经指定了安装目录,我们 -v、-V 查看下版本信息和编译信息1
/app/service/nginx/sbin/nginx -v
配置文件
基本结构
main # Nginx 运行相关的全局配置,对全局生效
├── events # 影响 Nginx 服务器或与用户的网络连接相关
├── http # 配置代理、缓存、日志记录、虚拟主机和第三方模块等
│ ├── upstream # 后端服务器具体地址,负载均衡配置不可或缺的部分
│ ├── server # 虚拟主机的相关参数,一个 http 块中可以有多个 server 块
│ ├── server
│ │ ├── location # 请求的路由,以及各种页面的处理情况
│ │ ├── location # server 块可以包含多个 location 块,location 指令用于匹配 uri
│ │ └── …
│ └── …
└── …语法规则
- 配置文件由指令与指令块构成
- 每条指令以“;”分号结尾,指令与参数间以空格符号分隔
- 指令块以 {} 大括号将多条指令组织在一起
- include 语句允许组合多个配置文件以提升可维护性
- 通过 ## 符号添加注释,提高可读性
- 通过 $ 符号使用变量
- 部分指令的参数支持正则表达式,例如常用的 location 指令
配置指令
return
返回 http 状态码和可选的第二个参数可以是重定向的 URL
- 状态码
- 状态码 & 文本
- 状态码 & 重定向地址
- 重定向地址
1
2
3
4
5
6
7
8
9
10
11
12 location / {
return 404;
}
location / {
return 404 "pages not found";
}
location / {
return 302 /bbs ;
}
location / {
return https://www.baidu.com ;
}rewrite
重写 URI 请求 rewrite,通过使用 rewrite 指令在请求处理期间多次修改请求 URI,该指令具有一个可选参数和两个必需参数
- 第一个(必需)参数是请求 URI 必须匹配的正则表达式
- 第二个参数是用于替换匹配 URI 的 URI
- 可选的第三个参数重写策略
- last 重写后的 URL 发起新请求,再次进入 server 段,重试 location 的中的匹配
- break 直接使用重写后的 URL ,不再匹配其它 location 中语句
- redirect 返回 302 临时重定向
- permanent 返回 301 永久重定向
1
2
3 location /users/ {
rewrite ^/users/(.*)$ /show?user=$1 break;
}error_page
使用error_page
指令,可以配置 Nginx 返回自定义页面以及错误代码,替换响应中的其他错误代码,或将浏览器重定向到其他 URI
1 error_page 500 502 503 504 /50x.html;日志
日志切割
1
2
3
4 #定义切割日志文件的方式
log_rotate daily;
#定义保存日志文件的天数
log_rotate_age 30;内置变量
常用的内置全局变量,可以在配置中随意使用
配置实例
配置文件:vi /app/conf/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51worker_processes auto;
error_log /app/logs/nginx/error.log notice;
events {
worker_connections 1024;
}
http {
include 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 /wechat/logs/nginx/error.log main;
sendfile on;
##tcp_nopush on;
##隐藏错误页版本号
server_tokens off;
client_max_body_size 100m;
client_body_buffer_size 128k;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;
types_hash_max_size 3096;
keepalive_timeout 60s;
keepalive_requests 80920;
##支持 WebSocket
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_proxied any;
gzip_types text/css application/javascript application/xml text/plain application/x-font-ttf ;
server {
server_name localhost;
listen 80;
##日志文件
access_log /app/logs/nginx/access.log main;
error_log /app/logs/nginx/error.log error;
if ($request_uri ~* .*\.\.\/.* ) {
return 403 "forbidden";
}
##默认主页
location / {
root html;
index index.html index.htm;
}
}
}
日常运维
开机自启
创建开机启动命令脚本文件(需要 ROOT 权限):vi /etc/init.d/nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# chkconfig: - 85 15
PATH=/app/service/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=/app/conf/$NAME.conf
PIDFILE=$PATH/$NAME.pid
scriptNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
其他命令
1 | ##创建软连接(root 权限运行) |
SSL 网站1
2
3
4
5
6
7
8
9server {
listen 443 ssl; ## TCP listener for HTTP/1.1
listen 443 http3 reuseport; ## UDP listener for QUIC+HTTP/3
ssl_protocols TLSv1.3; ## QUIC requires TLS 1.3
ssl_certificate ssl/www.example.com.crt;
ssl_certificate_key ssl/www.example.com.key;
add_header Alt-Svc 'quic=":443"'; ## Advertise that QUIC is available
add_header QUIC-Status $quic; ## Sent when QUIC was used
}