nginx常用操作
Easul Lv6

nginx与apache的比较

可以参考apache与nginx的简单对比

nginx编译安装

折叠代码块BASH 复制代码
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
# 安装基础所需依赖
sudo apt install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel make -y
# 下载nginx源码包并解压
wget http://nginx.org/download/nginx-1.19.6.tar.gz
tar -zxvf nginx-1.19.6.tar.gz
cd nginx-1.19.6
# 指定所需参数并编译, 编译过程如有报错, 则搜索错误信息并安装所需依赖
# 默认--sbin-path, --conf-path, --error-log-path, --http-log-path的路径都在--prefix下
./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/access.log \ # 设定access log路径
--pid-path=/var/run/nginx.pid \ # pid路径
--lock-path=/var/run/nginx.lock \ # 进程锁
-–modules-path=/usr/lib64/nginx/modules \ # 模块安装位置
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module

参考

nginx常用命令

折叠代码块BASH 复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
# 查看nginx可执行文件路径
ps -ef | grep nginx
# 查看nginx版本
/usr/local/nginx/sbin/nginx -v
/usr/local/nginx/sbin/nginx -V
# 查看nginx配置文件路径
/usr/local/nginx/sbin/nginx -t
# 启动
/usr/local/nginx/sbin/nginx
# 重启
/usr/local/nginx/sbin/nginx -s reload
# 停止
/usr/local/nginx/sbin/nginx -s stop

参考1
参考2

nginx简易教程

参考资料如下

参考教程为w3cschool编写的文档

nginx架构

  1. daemon方式运行, 有一个master进程和多个worker进程(nginx主流方式为多进程, 也可以使用多线程)
  2. master进程用来管理worker进程,
    1. master进程接收外界信号, 给worker发信号, 监控worker, 不执行任务
    2. worker异常退出, master会重启新worker
    3. worker进行请求的处理, 一个请求只在一个worker处理, 一个worker只处理一个请求
    4. worker个数一般为cpu核数
    5. worker之间是平等的, 且每个worker都是从master处fork过来的
  3. nginx重启或停止的几种方式
    折叠代码块BASH 复制代码
    1
    2
    3
    4
    5
    6
    7
    # -HUP  挂起进程, 挂起的同时会加载配置文件, -9是六亲不认杀死进程
    # 这里挂起的时候master加载配置文件, 并加载新的worker执行新请求, 老的worker处理完当前所有请求后, 直接退出
    kill -HUP pid
    # 重启
    ./nginx -s reload
    # 停止
    ./nginx -s stop

nginx常用配置模板

静态文件托管

这里是多个域名同时监听一个端口的配置

折叠代码块C 复制代码
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 在 http 块中配置如下代码
server {
# 监听端口
listen 80;
# 监听域名,可写多个,用空格分割
server_name example.com;

# 访问 80 就进行 301 跳转
return 301 https://$host$request_uri;
}

server {
listen 80;
server_name test.example.com;

return 301 https://$host$request_uri;
}


# example.com 域名访问 443 时的配置
server {
listen 443 ssl http2;
server_name example.com;

# SSL证书配置
ssl_certificate "/etc/nginx/cert/cert.pem";
ssl_certificate_key "/etc/nginx/cert/cert.key";

# 当 url 为 /popup 开头时,
# 从 /usr/share/nginx/html/popup-front-end 目录下 找
# /popup 开头的目录
location /popup {
# 托管的根目录
root "/usr/share/nginx/html/popup-front-end";
# 目录首页文件
index index.html;
# 尝试查找并提供请求的文件,找不到返回 404
try_files $uri $uri/ =404;
# 添加跨域头
add_header Access-Control-Allow-Origin * always;
}

# 当 url 为 / 开头时,
# 从 /usr/share/nginx/html/demo 目录下 找相关路径
location / {
root "/usr/share/nginx/html/demo";
index index.html;
try_files $uri $uri/ =404;
add_header Access-Control-Allow-Origin * always;
}
}

# test.example.com 域名访问 443 时的配置
server {
listen 443 ssl http2;
server_name test.example.com;

# SSL证书配置
ssl_certificate "/etc/nginx/cert/cert.pem";
ssl_certificate_key "/etc/nginx/cert/cert.key";

root /usr/share/nginx/html/mytest;
index index.html;
location / {
# 尝试查找并提供请求的文件
try_files $uri $uri/ =404;
add_header Access-Control-Allow-Origin * always;
}

}

反代某个端口

折叠代码块C 复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 在 http 块中配置如下代码
server {
listen 80;
server_name example.com;
# 80 跳转 https
return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
server_name example.com;

# SSL证书配置
ssl_certificate "/etc/nginx/cert/cert.pem";
ssl_certificate_key "/etc/nginx/cert/cert.key";

# 将HTTPS请求代理到本地的localhost:8080
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

websocket代理

也可以直接在windows下的nginx标准二进制包中直接使用

折叠代码块C 复制代码
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
# 在 http 块中配置如下代码
# 这个是反代的后端 websocket 的 ip + port
upstream backend {
server 127.0.0.1:8080;
}

# 使用 map 进行匹配
# map 指令用于根据请求头中的 Upgrade 字段的值
# 来映射到 $connection_upgrade 变量。
# 如果请求头中的 Upgrade 是默认值或空
# 则将 $connection_upgrade 设置为 close
# 否则设置为 upgrade。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {
listen 80;
server_name example.com;

return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
server_name example.com;

# 如果有转义字符,需要使用 \\
ssl_certificate C:\code\cert\\node.pem;
ssl_certificate_key C:\code\cert\\node.key;

# 设置 websocket 代理
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
 评论
来发评论吧~
Powered By Valine
v1.5.2