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
wget http://nginx.org/download/nginx-1.19.6.tar.gz tar -zxvf nginx-1.19.6.tar.gz cd nginx-1.19.6
./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 \ --pid-path=/var/run/nginx.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
| ps -ef | grep nginx
/usr/local/nginx/sbin/nginx -v /usr/local/nginx/sbin/nginx -V
/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架构
- 以
daemon方式运行, 有一个master进程和多个worker进程(nginx主流方式为多进程, 也可以使用多线程)
master进程用来管理worker进程,
- master进程接收外界信号, 给worker发信号, 监控worker, 不执行任务
worker异常退出, master会重启新worker
- worker进行请求的处理, 一个请求只在一个worker处理, 一个worker只处理一个请求
- worker个数一般为cpu核数
- worker之间是平等的, 且每个worker都是从master处fork过来的
- nginx重启或停止的几种方式
折叠代码块BASH
复制代码
1 2 3 4 5 6 7
|
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: }
server { listen 80; server_name test.example.com; return 301 https: }
# 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: }
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: 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; } }
|
v1.5.2