Centos7.9源码编译nginx-1.22.1+Nginx-Fancyindex模块美化浏览目录

Centos7.9源码编译nginx-1.22.1+Nginx-Fancyindex模块美化浏览目录

upload successful

1、安装Nginx所需要的依赖


# yum install -y gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel

# cd /acdata/dba_tools/nginx_source
# wget https://nginx.org/download/nginx-1.22.1.tar.gz
# tar xvfz nginx-1.22.1.tar.gz
# cd nginx-1.22.1
# wget https://github.com/aperezdc/ngx-fancyindex/releases/download/v0.5.2/ngx-fancyindex-0.5.2.tar.xz
# tar xvf ngx-fancyindex-0.5.2.tar.xz

2、执行配置: 添加http和https模块和ngx-fancyindex模块

--prefix为指定安装路径,不指定默认安装到/usr/local/nginx目录
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=ngx-fancyindex-0.5.2

3、对Nginx编译和安装

# make & make install

:::tip{title=”提示”}
看到如下信息,说明编译安装成功:

test -f '/usr/local/nginx/conf/scgi_params' \
|| cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/acdata/dba_tools/nginx_source/nginx-1.22.1'

:::

4、修改Nginx配置和打包以便后续解压直接使用,省去打包编译。

Fancy Index 配置

  • 1、进入Nginx安装的web目录,执行nginx -V,输出configure arguments: –prefix=/usr/local/nginx,就是这个目录
  • 2、git clone https://github.com/lanffy/Nginx-Fancyindex-Theme.git
  • 3、在nginx location模块中添加Fancy Index配置,如下:
location /download
{
include /usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf; # 目录美化配置
root /home/map/www/; #指定目录所在路径
autoindex on; #开启目录浏览
autoindex_format html; #以html风格将目录展示在浏览器中
autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
autoindex_localtime on; #以服务器的文件时间作为显示的时间
charset utf-8,gbk; #展示中文文件名
}
  • 4、重启Nginx即可

4.1、 编辑nginx.conf配置文件:

[root@wtj1vpdbmgnt01 conf]# cat nginx.conf

#user nobody;
worker_processes 3;

error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

#index index.html index.htm;

#root /acdata/dba_tools/shell/check_mysql_report_news/inspection_data_report;

#支持下载功能
location /download{
alias /acdata/dba_tools/shell/check_mysql_report_news/inspection_data_report;
#include /usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf; # 目录美化配置
fancyindex on; # Enable fancy indexes.
fancyindex_exact_size off; # Output human-readable file sizes.
autoindex on; #开启目录浏览
autoindex_format html; #以html风格将目录展示在浏览器中
autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
autoindex_localtime on; #以服务器的文件时间作为显示的时间
charset utf-8,gbk; #展示中文文件名

#添加同时支持下载和预览文件功能
if ($request_filename ~* ^.*?\.(html|doc|pdf|zip|docx|txt|conf|gz|xz|rar|sql)$) {
add_header Content-Disposition attachment;
add_header Content-Type application/octet-stream;
}

#密码,按需开启
auth_basic "开源服务-数据库巡检报告访问验证";
auth_basic_user_file /etc/nginx/passwd; # 自定义一个绝对路径的密码文件
}

#支持在线预览功能
location /report{
alias /acdata/dba_tools/shell/check_mysql_report_news/inspection_data_report;
#include /usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf; # 目录美化配置
fancyindex on; # Enable fancy indexes.
fancyindex_exact_size off; # Output human-readable file sizes.
autoindex on; #开启目录浏览
autoindex_format html; #以html风格将目录展示在浏览器中
autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
autoindex_localtime on; #以服务器的文件时间作为显示的时间
charset utf-8,gbk; #展示中文文件名

#密码,按需开启
auth_basic "开源服务-数据库巡检报告访问验证";
auth_basic_user_file /etc/nginx/passwd; # 自定义一个绝对路径的密码文件
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

4.2、 编辑nginx.service服务文件:

[root@wtj1vpdbmgnt01 nginx-1.22.1]# cat /usr/local/nginx/conf/nginx.service 
[Unit]
Description= Nginx-1.22.1
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

4.3、打压缩包,方便后续重装直接使用,省去编译和配置等工作,直接解压使用。

# 进入到/usr/local目录
# cd /usr/local/
# 执行打包命令
# tar -czvf nginx-1.22.1-build_finished.tar.gz nginx

4.4、验证nginx服务是否可以正常启动

# 进入Nginx启动目录
# cd /usr/local/nginx/sbin/

# 启动Nginx服务
[root@localhost sbin]# ./nginx

# 重启Nginx服务
[root@localhost sbin]# ./nginx -s reload

# 关闭Nginx服务
[root@localhost sbin]# ./nginx -s stop

# 指定配置文件启动
[root@localhost sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf

# 查看Nginx是否启动成功
[root@localhost sbin]# ps -ef | grep nginx

4.5 配置Nginx服务管理和开机启动

# 关闭Nginx服务,再配置服务管理
[root@localhost sbin]# ./nginx -s stop

# 将开始编写的nginx.service文件复制到服务管理目录
[root@localhost sbin]# cp /usr/local/nginx/conf/nginx.service /etc/systemd/system/

# 启动Nginx服务
[root@localhost sbin]# systemctl start nginx

# 查看Nginx服务状态,显示active(running)即表示成功
[root@localhost sbin]# systemctl status nginx

# 设置Nginx开机启动
[root@localhost sbin]# systemctl enable nginx

# 开放Nginx服务80端口
[root@localhost sbin]# firewall-cmd --zone=public --add-port=80/tcp --permanent

# 重启防火墙
[root@localhost sbin]# firewall-cmd --reload

# 查看开放端口
[root@localhost sbin]# firewall-cmd --list-ports