1.环境准备:
文件包:nginx-1.26.3.tar.gz
文件包下载地址:https://nginx.org/en/download.html
镜像源:https://developer.volcengine.com/articles/7109734973568974855
2.安装配置:
2.1 上传文件包并解压:
root@localhost:~# ls
nginx-1.26.3.tar.gz
root@localhost:~# tar -xf nginx-1.26.3.tar.gz ##解压nginx文件包
root@localhost:~# cd nginx-1.26.3/ ##进入解压后的nginx目录内
2.2 安装依赖:
root@localhost:~# apt-get update ##更新本地的软件包
root@localhost:~# apt install build-essential libtool libpcre3 libpcre3-dev zlib1g-dev libssl-dev ##安装nginx所需要的依赖包
2.3 编译安装:
root@localhost:~#: ls ##解压后的目录源文件
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
root@localhost:~#: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
##编译安装
root@localhost:~#: make && make install ##编译安装
编译安装配置项详解:
--prefix=/usr/local/nginx ## 指定nginx部署目录
--user=nginx --group=nginx ## 指定nginx进程运行时使用的用户和组
--with-http_ssl_module ## 启用 HTTPS/SSL 支持(80/443)
--with-http_v2_module ## 支持 HTTP/2 协议,提示网页加载性能
--with-http_realip_module ## 开启 显示客户端真实IP,如 CDN或者代理
--with-http_stub_status_module ## 启用 Nginx 状态监控页面
--with-http_gzip_static_module ## 启用 gzip 压缩
--with-pcre ## 启用 PCRE 库支持正则表达式
--with-http_rewrite_module ## 支持 URL 重写
2.4 启动Nginx服务:
root@localhost:~# /usr/local/nginx/sbin/nginx ##启动nginx服务
root@localhost:~# groupadd nginx ##创建nginx用户组
root@localhost:~# useradd -g nginx -s /sbin/nologin nginx ##创建nginx用户,并禁止登录
root@localhost:~# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx ##设置nginx软连接
root@localhost:~# nginx ##再次启动nginx服务
root@localhost:~# nginx -V ##查看nginx版本及模块
root@localhost:~# netstat -nutlp | grep 80 ##过滤nginx服务端口
2.5 nginx配置文件:
root@localhost:~# vim /usr/local/nginx/conf/nginx.conf
#user nobody; # nginx服务运行时使用的用户
worker_processes auto; # nginx工作进程数量
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; # 错误日志路径,及输出日志等级
# info debug warn error
#pid logs/nginx.pid; # nginx 进程ID文件位置
events{
worker_connections 65535; # 设置每个Worker进程能够同时处理的最大连接数,提示nginx并发
}
http {
include mime.types; # 加载MIME类型定义文件(扩展名到Content-Type的映射)
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 /dev/null; # 访问日志文件位置
sendfile on; # 启用零拷贝传输(提升静态文件性能)
keepalive_timeout 65; # 客户端长连接保持65秒(减少TCP握手开销)
##gzip压缩配置
gzip on; # 启用Gzip压缩
gzip_http_version 1.1; # 仅压缩HTTP/1.1及以上协议
gzip_min_length 1k; # 只压缩大于1KB的文件
gzip_buffers 4 16k; # 压缩缓冲区大小(4个16KB内存块)
gzip_comp_level 9; # 最高压缩级别(CPU消耗较高)
gzip_types text/plain [...] # 指定压缩的文件类型(覆盖了常见Web资源)
gzip_vary on; # 添加"Vary: Accept-Encoding"响应头
gzip_disable "MSIE [1-6]\."; # 禁用IE6及以下版本的Gzip(兼容性)
##代理配置
proxy_set_header Host $host; # 传递原始请求的Host头
proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 代理链IP记录
proxy_set_header X-Forwarded-Proto $scheme; # 原始协议(http/https)
##跨域配置
add_header 'Access-Control-Allow-Origin' *; # 允许所有域跨域访问
add_header 'Access-Control-Allow-Credentials' 'true'; # 允许携带Cookie
add_header 'Access-Control-Allow-Methods' *; # 允许所有HTTP方法
add_header 'Access-Control-Allow-Headers' *; # 允许所有请求头
server {
listen 80;
server_name test.cn; # 匹配请求的Host头
##前端页面配置
location / {
root /usr/local/nginx/html/test_kehu; # 静态文件根目录
index index.html index.htm; # 默认索引文件
try_files $uri $uri/ /index.html; # 前端路由支持(如Vue/React)
}
##前端路由histroy模式
location /mobile { # 独立移动端路径
alias /usr/local/nginx/html/test_mobile; # 注意:alias与root的区别
try_files $uri $uri/ /index.html;
}
##nginx状态监控页
location /nginx_status { # Nginx基础状态监控
stub_status; # 返回简版状态页
access_log off; # 不记录访问日志
allow 127.0.0.1; # 建议添加IP白名单限制!
deny all;
}
}
server {
listen 443 ssl;
server_name test.cn;
##SSL证书配置
ssl_certificate ../cert/test.cn.pem; # SSL证书路径(建议使用绝对路径)
ssl_certificate_key ../cert/test.key; # SSL私钥路径
ssl_session_timeout 5m; # SSL会话缓存超时时间
ssl_protocols TLSv1.2 TLSv1.3; # 仅允许安全协议(禁用TLSv1.0/1.1)
ssl_ciphers ECDHE+AESGCM:ECDHE+CHACHA20:...; # 现代加密套件
ssl_prefer_server_ciphers on; # 优先使用服务端加密配置
ssl_session_cache shared:SSL:10m; # SSL会话缓存(提升性能)
client_max_body_size 500M; # 允许上传大文件(如视频)
##80端口强制跳转443
add_header Strict-Transport-Security "max-age=31536000"; # 强制HTTPS(HSTS)
if ($server_port !~ 443){ # HTTP自动跳转HTTPS
rewrite ^(/.*)$ https://$host$1 permanent;
}
## 接口代理
location /oaapi/ { # 反向代理到后端Java服务
proxy_pass http://192.168.1.1:8080/; # 注意末尾斜杠(会去除/oaapi前缀)
}
location /testapi/ {
proxy_pass http://api/;
}
# 静态文件缓存至用户浏览器
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
## 永久重定向www到非www
if ($host = 'www.example.com') {
return 301 https://example.com$request_uri;
}
## 重定向旧域名到新域名
if ($host = 'old.example.com') {
return 301 https://example.com$request_uri;
}
# 重定向HTTP到HTTPS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # 强制HTTPS(HSTS)
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
## 加权轮询代理
upstream api {
server 192.168.0.1:8080 weight=2;
server 192.168.0.2:8080 weight=1;
}
}
3.nginx命令及目录信息
3.1 nginx基础命令
root@localhost:~# nginx -t #检测配置文件格式
root@localhost:~# nginx -s reload # nginx热加载配置文件
root@localhost:~# nginx -s stop # 停止nginx服务
root@localhost:~# nginx -V # 查看版本及模块信息
## 如果未执行2.4里的软连接命令,会提示命令不存在,需执行绝对路径 /usr/local/nginx/sbin/nginx
3.2 nginx目录信息
/usr/local/nginx # nginx服务目录
/usr/local/nginx/sbin/nginx # nginx可执行命令目录
/usr/local/nginx/conf # 默认配置文件目录,及SSL证书目录
/usr/local/nginx/conf/nginx.conf # 配置文件
/usr/local/nginx/html # nginx默认前端文件目录
/usr/local/nginx/logs # nginx日志文件目录
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容