017 Nginx
2025/6/6约 1937 字大约 6 分钟
Linux 安装 Nginx
# 直接使用 dnf 源安装
dnf install -y nginx
# 查看/重启/开启/自启动/关闭自启动 Nginx 服务
systemctl status[ restart | start | enable | disable ] nginx
# 编译安装需要查看编译选项
TODO配置 Nginx HTTPS 服务器配置
提示
默认配置文件: /etc/nginx/nginx.conf
默认站点目录: /usr/share/nginx/html
参考配置:
- 配置了 HTTP 服务器 80 端口
- 配置了 HTTP 服务器 /download 目录为 Nginx 的文件下载页面,并使用第三方模块 fancyindex 美化页面
- 配置了 HTTPS 服务器加密访问,并配置了自签名证书
HTTP 配置参考
# /etc/nginx/nginx.conf
# 有关配置的更多信息, 请参阅:
# * 官方英语文档: http://nginx.org/en/docs/
# * 官方俄语文档: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;
# 加载动态模块, 请参见 /usr/share/doc/nginx/README.dynamic
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 从 /etc/nginx/conf.d 目录加载模块化配置文件
# 参考 http://nginx.org/en/docs/ngx_core_module.html#include 获取更多信息
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# # 启用本机验证方式
# auth_basic "User Authentication";
# # 修改使用 htpasswd 命令生成用户名/密码的文件路径
# auth_basic_user_file /usr/local/nginx-1.10.2/conf/pass.db;
# 使用HTTP时重定向到HTTPS,如果server_name为域名则此处使用$server_name替换$host(IP相关)
# return 301 https://$host$request_uri;
# 加载默认服务器模块的配置文件
include /etc/nginx/default.d/*.conf;
location /download {
# 使用Nginx自带模块开启目录索引
# autoindex on;
# 显示文件大小 KB,MB,GB
# autoindex_exact_size off;
# 显示本机时间
# autoindex_localtime on;
# 以下为使用第三方模块进行序列号文件下载服务器的模块设置
# 使用第三方模块进行目录索引,功能更强大
fancyindex on;
# 不显示精确大小
fancyindex_exact_size off;
# 文件日期显示格式
fancyindex_time_format "%Y-%m-%d %H:%M:%S";
# 使用用户本地时间显示
fancyindex_localtime on;
# 名称显示长度
fancyindex_name_length 255;
# 自定义主题页面头
# fancyindex_header "/Theme/fancyindex-theme-light-001/header.html";
# 页面尾
# fancyindex_footer "/Theme/fancyindex-theme-light-001/footer.html";
# 不好看,还是 fancyindex 自带样式好看
# fancyindex_header "/Theme/Nginx-Fancyindex-Theme-dark/header.html";
# fancyindex_footer "/Theme/Nginx-Fancyindex-Theme-dark/footer.html";
# 忽略的文件夹/文件
fancyindex_ignore "Theme";
# 解决中文显示乱码问题
charset utf-8;
# 对于匹配中的文件类型,以附件形式下载而不是浏览
if ($request_filename ~* ^.*?\.(txt|jpg|png)$) {
add_header Content-Disposition 'attachment';
}
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# 启用 TLS 的服务器的设置
#
# server {
# listen 443 ssl;
# listen [::]:443 ssl;
# server_name _;
# root /usr/share/nginx/html;
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# # ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
# # 加载默认服务器模块的配置文件
# include /etc/nginx/default.d/*.conf;
# error_page 404 /404.html;
# location = /40x.html {
# }
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}HTTPS 配置参考
# /etc/nginx/nginx.conf
# 有关配置的更多信息, 请参阅:
# * 官方英语文档: http://nginx.org/en/docs/
# * 官方俄语文档: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;
# 加载动态模块, 请参见 /usr/share/doc/nginx/README.dynamic
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 从 /etc/nginx/conf.d 目录加载模块化配置文件
# 参考 http://nginx.org/en/docs/ngx_core_module.html#include 获取更多信息
include /etc/nginx/conf.d/*.conf;
server {
# 使用HTTP时重定向到HTTPS,如果server_name为域名则此处使用$server_name替换$host(IP相关)
return 301 https://$host$request_uri;
# # 启用本机验证方式
# auth_basic "User Authentication";
# # 修改使用 htpasswd 命令生成用户名/密码的文件路径
# auth_basic_user_file /usr/local/nginx-1.10.2/conf/pass.db;
}
# 启用 TLS 的服务器的设置
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name _;
root /usr/share/nginx/html;
ssl_certificate "/etc/pki/nginx/server.pem";
ssl_certificate_key "/etc/pki/nginx/private/server.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
ssl_prefer_server_ciphers on;
# 加载默认服务器模块的配置文件
include /etc/nginx/default.d/*.conf;
location /download {
# 使用Nginx自带模块开启目录索引
# autoindex on;
# 显示文件大小 KB,MB,GB
# autoindex_exact_size off;
# 显示本机时间
# autoindex_localtime on;
# 以下为使用第三方模块进行序列号文件下载服务器的模块设置
# 使用第三方模块进行目录索引,功能更强大
fancyindex on;
# 不显示精确大小
fancyindex_exact_size off;
# 文件日期显示格式
fancyindex_time_format "%Y-%m-%d %H:%M:%S";
# 使用用户本地时间显示
fancyindex_localtime on;
# 名称显示长度
fancyindex_name_length 255;
# 自定义主题页面头
# fancyindex_header "/Theme/fancyindex-theme-light-001/header.html";
# 页面尾
# fancyindex_footer "/Theme/fancyindex-theme-light-001/footer.html";
# 不好看,还是 fancyindex 自带样式好看
# fancyindex_header "/Theme/Nginx-Fancyindex-Theme-dark/header.html";
# fancyindex_footer "/Theme/Nginx-Fancyindex-Theme-dark/footer.html";
# 忽略的文件夹/文件
fancyindex_ignore "Theme";
# 解决中文显示乱码问题
charset utf-8;
# 对于匹配中的文件类型,以附件形式下载而不是浏览
# if ($request_filename ~* ^.*?\.(txt|jpg|png)$) {
# add_header Content-Disposition 'attachment';
# }
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}编译、安装、使用第三方模块 ngx-fancyindex-0.5.2 美化页面
ngx-fancyindex 模块的安装,需要在编译 Nginx 时将该模块加进去
相关信息
ngx-fancyindex 模块官网: https://github.com/aperezdc/ngx-fancyindex
ngx-fancyindex 的第三方响应式主题: https://github.com/Naereen/Nginx-Fancyindex-Theme
- 解压 ngx-fancyindex.tar.gz ,记住解压后的目录
- 根据 .spec 文件内容,新增编译选项
--add-module=/home/mintimate/ngx-fancyindex-0.5.1,通过 rpmbuild -ba 命令编译为 rpm 二进制包 - 或者在Linux环境下通过
make && make install命令编译安装(此种方式建议完全卸载之前通过 .rpm 安装的 Nginx )# make && make install 参考构建参数 ./configure \--prefix=/etc/nginx \--sbin-path=/usr/sbin/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 \--http-client-body-temp-path=/var/cache/nginx/client_temp \--http-proxy-temp-path=/var/cache/nginx/proxy_temp \--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \--http-scgi-temp-path=/var/cache/nginx/scgi_temp \--user=nginx \--group=nginx \--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=/home/mintimate/ngx-fancyindex-0.5.1[模块路径] make install - 然后配置
/etc/nginx/nginx.conf文件即可
为 fancyindex 模块添加主题
- 下载 ./res/018/ 主题资源,并解压至【网站根目录】
- 配置文件 /etc/nginx/nginx.conf 添加如下配置配置
# 自定义主题 # 页头文件路径,相对于网站根目录 fancyindex_header "/Nginx-Fancyindex-Theme-dark/header.html"; # 页尾文件路径,相对于网站根目录 fancyindex_footer "/Nginx-Fancyindex-Theme-dark/footer.html"; # 需要忽略的文件夹/文件,可以把主题目录隐藏 fancyindex_ignore "Nginx-Fancyindex-Theme-dark"; fancyindex_ignore ""; # 文件名显示的最大长度 fancyindex_name_length 255;
为网站页面添加访问认证
提示
有 2 种方式添加访问认证:
- 基于本机
- 第三方模块和服务。
此处仅讨论学习第一种, 基于本机的用户名/密码访问认证
# 1. 下载 httpd-tools 软件包,用于获取生成 用户名-密码 文件的命令 htpasswd
dnf install httpd-tools
# 2. 使用 htpasswd 命令生成 用户名-密码 文件
htpasswd -5[采用SHA-512加密] -b[使用命令行中密码] -c[为该用户创建新文件,去掉则追加] pass.db[保存认证信息的文件] wang[用户名] ABC[密码]
htpasswd -5 -b -c passwd.db wang ABC
# 3. 修改 nginx.conf 配置文件,放在 http{ server{ root xxx;下方即可 } }
auth_basic "User Authentication";
auth_basic_user_file /etc/nginx/passwd.db;清除系统所有防火墙安全策略
- 清除 iptables(端口防火墙)配置
iptables -F iptables -F -t nat iptables -X iptables -X -t nat - 关闭 selinux(系统安全策略),见QA.1
查看使用某端口的进程和服务
# 替换想查看的端口号
lsof -i:80常见问题记录
1
Q:更换server端口,却无法启动nginx服务。查看 /var/log/nginx/error.log 日志文件,报错:
[emerg] 426437#0: bind() to 0.0.0.0:82 failed (13: Permission denied)A:可能是 selinux 安全策略导致
# 1. 查询selinux状态,若输出 Enforcing,说明开启中。2、3方法任选其一,建议2(临时关闭)
getenforce
# 2. 临时关闭
setenforce 0
# 3. 永久关闭,编辑/etc/selinux/config文件,将SELINUX=disabled
SELINUX=disabled