CentOS-7.9;nginx version: nginx/1.24.0
worker_processes auto;
events {
# 每个工作进程的连接数,默认为1024个
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
charset gbk;
gzip on;
server {
listen 80;
server_name localhost.zglsyjy.com;
root /data/download; # 文件存放目录
location / {
autoindex on; # 启用自动首页功能
autoindex_format html; # 首页格式为HTML
autoindex_exact_size on; # 文件大小自动换算
autoindex_localtime on; # 按照服务器时间显示文件时间
# default_type application/octet-stream;# 将当前目录中所有文件的默认MIME类型设置为
# # application/octet-stream
# if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$) {
# # 当文件格式为上述格式时,将头字段属性Content-Disposition的值设置为"attachment"
# add_header Content-Disposition: 'attachment;';
# }
sendfile on; # 开启零复制文件传输功能
sendfile_max_chunk 1m; # 每个sendfile调用的最大传输量为1MB
tcp_nopush on; # 启用最小传输限制功能
aio on; # 启用异步传输
directio 5m; # 当文件大于5MB时以直接读取磁盘的方式读取文件
directio_alignment 4096; # 与磁盘的文件系统对齐
output_buffers 4 32k; # 文件输出的缓冲区大小为128KB
limit_rate 1024m; # 限制下载速度为1MB
limit_rate_after 1024m; # 当客户端下载速度达到2MB时进入限速模式
max_ranges 4096; # 客户端执行范围读取的最大值是4096B
send_timeout 20s; # 客户端引发传输超时时间为20s
postpone_output 2048; # 当缓冲区的数据达到2048B时再向客户端发送
chunked_transfer_encoding on; # 启用分块传输标识
}
}
}
worker_processes auto;
events {
# 每个工作进程的连接数,默认为1024个
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
charset gbk;
gzip on;
server {
listen 443 ssl;
server_name xxxxxxx.com;
root /data/download; # 文件存放目录
ssl_certificate /etc/nginx/cert/xxxxxxxxxx.com.pem;
ssl_certificate_key /etc/nginx/cert/xxxxxxxxxx.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
location / {
autoindex on; # 启用自动首页功能
autoindex_format html; # 首页格式为HTML
autoindex_exact_size on; # 文件大小自动换算
autoindex_localtime on; # 按照服务器时间显示文件时间
# default_type application/octet-stream;# 将当前目录中所有文件的默认MIME类型设置为
# # application/octet-stream
# if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$) {
# # 当文件格式为上述格式时,将头字段属性Content-Disposition的值设置为"attachment"
# add_header Content-Disposition: 'attachment;';
# }
sendfile on; # 开启零复制文件传输功能
sendfile_max_chunk 1m; # 每个sendfile调用的最大传输量为1MB
tcp_nopush on; # 启用最小传输限制功能
aio on; # 启用异步传输
directio 5m; # 当文件大于5MB时以直接读取磁盘的方式读取文件
directio_alignment 4096; # 与磁盘的文件系统对齐
output_buffers 4 32k; # 文件输出的缓冲区大小为128KB
limit_rate 1024m; # 限制下载速度为1MB
limit_rate_after 1024m; # 当客户端下载速度达到2MB时进入限速模式
max_ranges 4096; # 客户端执行范围读取的最大值是4096B
send_timeout 20s; # 客户端引发传输超时时间为20s
postpone_output 2048; # 当缓冲区的数据达到2048B时再向客户端发送
chunked_transfer_encoding on; # 启用分块传输标识
}
}
}
一、安装生成密码工具
在线安装
安装htpasswd工具:
(yum安装):
yum -y install httpd-tools
离线安装
rpm -ivh httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
依赖于:
apr-1.4.8-7.el7.x86_64.rpm
apr-util-1.5.2-6.el7.x86_64.rpm
二、生成密码文件
设置用户名和密码,并把用户名和密码保存到指定文件中:注意保存路径
htpasswd -cb /etc/nginx/conf.d/htpasswd Username password
三、查看密码文件
cat /etc/nginx/conf.d/htpasswd
admin:$apr1$cUb6/3Va$reEEsLeVjG9IQMO19jedO/
其中admin是用户名,分号后面就是密码(密码已经加过密)
四、修改配置文件拦截站点或请求
找到nginx配置文件所在位置文件中server中location添加:
密码提示语|密码文件路径
server {
.......
auth_basic "Username and Password are required";
auth_basic_user_file /etc/nginx/conf.d/htpasswd;
}
需要对server限制访问也可加在server模块
五、重启nginx并验证是否访问受限
验证配置文件是否正确
nginx -t
重启服务
nginx -s reload
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /download {
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
alias /etc/nginx/download;
auth_basic "Username and Password are required";
auth_basic_user_file /etc/nginx/conf.d/htpasswd;
}
location /status {
stub_status on;
access_log off;
}
#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 /etc/nginx/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;
#}
}
[root@env-test2 ~]#nkvers
############## Kylin Linux Version #################
Release:
Kylin Linux Advanced Server release V10 (Sword)
Kernel:
4.19.90-25.38.v2101.ky10.x86_64
Build:
Kylin Linux Advanced Server
release V10 (SP2) /(Sword)-x86_64-Build09/20210524
#################################################
查看:https://blog.csdn.net/weixin_43332972/article/details/141164651?spm=1001.2014.3001.5502
1 拉取nginx镜像:
[root@env-test2 /etc/nginx/conf]#docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a480a496ba95: Pull complete
f3ace1b8ce45: Pull complete
11d6fdd0e8a7: Pull complete
f1091da6fd5c: Pull complete
40eea07b53d8: Pull complete
6476794e50f4: Pull complete
70850b3ec6b2: Pull complete
Digest: sha256:28402db69fec7c17e179ea87882667f1e054391138f77ffaf0c3eb388efc3ffb
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
2 查看镜像是否拉取成功:
[root@env-test2 ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 3b25b682ea82 4 weeks ago 192MB
3 创建启动容器需映射挂载的目录及文件:
[root@env-test2 ~]# mkidr /etc/nginx/{conf.d,download} -p
[root@env-test2 ~]# vim /etc/nginx/nginx.conf
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
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 2048;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/conf.d/*.conf;
# limit_rate 50k;# 限制下载速度
location / {
root /usr/share/nginx/html/download;
autoindex on; #开启索引功能
autoindex_exact_size off; #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #显示本机时间而非 GMT 时间
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
4 启动容器:
[root@env-test2]#docker run -itd --name mynginx -p 81:80 -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf -v /etc/nginx/download:/usr/share/nginx/html/download -v /etc/nginx/conf.d:/etc/nginx/conf.d --privileged nginx
5 查看容器启动状态,查看容器日志
[root@env-test2]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1520ccd8cd42 nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp mynginx
[root@env-test2]# docker logs -f mynginx
7 同步容器时间:
[root@env-test2 /etc/nginx]#docker exec -it mynginx bash
root@1520ccd8cd42:/# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@1520ccd8cd42:/# exit
[root@env-test2 /etc/nginx]#docker restart mynginx
四 浏览器访问:http:// ip:port
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。