一、下载安装
Nginx 有几个模块,实际项目中经常使用,但在编译安装的时候默认是不安装的,需要加入命令选项。当然,如果刚开始没有安装,后续也是可以再次编译替换的。具体选项配置可以参阅官网文档:http://nginx.org/en/docs/configure.html
# 支持 ssl 即 HTTPS
--with-http_ssl_module
# HTTP2
--with-http_v2_module
# 修改替换相应的字符串
--with-http_sub_module
# gunzip 压缩
--with-http_gunzip_module
# gzip
--with-http_gzip_static_module
# 提供获得基本状况的信息
--with-http_stub_status_module
以上几个模块默认都是不安装的。如果目前已经存在线上运行的 Nginx 服务,并且缺少上面的某些模块,那么再次下载和线上版本一致的 Nginx,然后重新编译(不需要安装),把需要用到的模块编译进去,然后替换源文件(nginx)即可。(如果线上版本较低,顺带想要升级,可以直接编译安装,覆盖原来的,注意 配置备份)。
这里就以更新为主,新安装的比较简单,编译完成后直接安装(make install)。
# 安装依赖包
sudo apt install libpcre3 libpcre3-dev zlib1g-dev libssl-dev build-essential -y
# 下载新的源文件
wget https://nginx.org/download/nginx-1.24.0.tar.gz
# 解压后进入解压目录
tar -xzvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 配置
./configure --prefix=/usr/local/nginx \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gunzip_module \
--with-http_gzip_static_module
# 编译
make
经过以上步骤后,在该目录下(.../xx/nginx-1.24.0)会多出来一个 objs 的目录,该目录下有一个 nginx 文件,这个就是新编译可执行文件。现在有两种选择:备份目前正在使用的 .../sbin/nginx 文件,然后拷贝新生成的 nginx 到该目录下;或者直接替换掉旧版本(最好版本一致时这样做)。
二、命令
# 停止
./nginx -s stop
# 查看配置文件的状态是否可用
./nginx -t
# 修改配置文件后重新加载配置文件
./nginx -s reload
三、配置
Nginx 的 location 块中可以使用两种配置方式。一种是 root,一种是 alias。注意,alias 只能在 location 中使用。
...
location /share {
root /data;
}
location /test {
alias /data/share/;
}
...
上面的配置可以访问:http://localhost:port/share、http://localhost:port/test 查看,两种配置方式在地址栏访问路径不同,但是访问的结果却是相同的。那么我们可以得到一个结论,alias 后跟的是资源的绝对路径,而 root 实际访问时会自动加上 location 后的目录。
location 后只是使用 http 访问时的别名。
另外根据官方文档的示例,root 方式可以不用在路径最后添加
/;alias 则必须添加。
2. 自定义配置
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 日志打印
log_format main '$time_iso8601|$remote_addr|$remote_user|$request_method|$uri|'
'$status|$request_time|$request_length|$body_bytes_sent|$bytes_sent|'
'$connection|$http_x_forwarded_for|$upstream_addr|$upstream_status|'
'$upstream_response_time|$args|$http_referer|$http_user_agent';
access_log logs/access.log main;
# 负载均衡服务器
upstream server_name {
# 设置权重,权重越高,使用此服务器的概率越大
# 8080 出现的频率是其它服务器的两倍
server localhost:8080 weight=2;
server localhost:8081;
server localhost:8082;
}
# 前后端分离项目部署
server {
listen 81;
server_name localhost;
location / {
root /usr/share/nginx/html/dist;
# 刷新 404
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 后端地址
# 如果时 podman 容器中的 nginx 这里需要使用宿主机 ip,否则无法访问
proxy_pass http://localhost:8888/;
}
}
server {
listen 80;
server_name localhost;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location / {
# 使用负载均衡服务器,默认策略轮询
proxy_pass http://server_name/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 配置静态资源
location ~ .*\.(css|gif|ico|jpg|js|png|ttf|woff)$ {
expires 24h;
# 指定图片存放路径
root D:/image/;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
# 图片路径
proxy_temp_path D:/image/;
proxy_redirect off;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
cl ient_max_body_size 10m;
client_body_buffer_size 1280k;
proxy_connect_timeout 900;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 40k;
proxy_buffers 40 320k;
proxy_busy_buffers_size 640k;
proxy_temp_file_write_size 640k;
if ( !-e $request_filename) {
proxy_pass http://127.0.0.1:80; #代理访问地址
}
}
}
# 使用 nginx 映射文件服务器
server {
listen 8181;
server_name localhost;
# 配置跨域
# add_header Access-Control-Allow-Origin *;
# add_header Access-Control-Allow-Headers X-Requested-With;
# add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
# charset koi8-r;
# access_log logs/host.access.log main;
location /share {
# /share 的上级全目录
root /data;
# 如果使用 alias,最后一定要加 /,而且只能用于 location 块
# alias /data/share/
# 显示索引序号
autoindex on;
# 可读方式显示文件大小,单位为 KB、MB 或者 GB
autoindex_exact_size off;
# 显示文件的时间
autoindex_localtime on;
# 设置默认传输的格式,文本可以直接在 浏览器 打开
default_type text/plain;
# 设置字符集
charset UTF-8;
}
}
}
若修改配置文件后再次启动报错:
[emerg]: bind() to 0.0.0.0:80 failed (80: Address already in use)执行命令:sudo fuser -k 80/tcp,然后重新启动./nginx
四、附
1. 常用的 nginx 全局变量
| 变量名称 | 含义 |
|---|---|
| $args | 请求行中的参数。 |
| $content_length | 请求头中的Content-length字段。 |
| $content_type | 请求头中的Content-Type字段。 |
| $document_root | 当前请求在root指令中指定的值。 |
| $host | 请求主机头字段,否则为服务器名称。 |
| $http_user_agent | 客户端agent信息 |
| $http_cookie | 客户端cookie信息 |
| $limit_rate | 这个变量可以限制连接速率。 |
| $request_body_file | 客户端请求主体信息的临时文件名。 |
| $request_method | 客户端请求的动作,通常为GET或POST。 |
| $remote_addr | 客户端的IP地址。 |
| $remote_port | 客户端的端口。 |
| $remote_user | 已经经过Auth Basic Module验证的用户名。 |
| $request_filename | 当前请求的文件路径,由root或alias指令与URI请求生成。 |
| $query_string | 与$args相同。 |
| $scheme | HTTP方法(如http,https)。 |
| $server_protocol | 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。 |
| $server_addr | 服务器地址,在完成一次系统调用后可以确定这个值。 |
| $server_name | 服务器名称。 |
| $server_port | 请求到达服务器的端口号。 |
| $request_uri | 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。 |
| $uri | 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。 |
| $document_uri | 与 $uri 相同。 |

说些什么吧!