【B站】Nginx部署实战教程 最后更新:2025年08月31日 作者:管理员 > 视频地址:https://www.bilibili.com/video/BV1hit1zeEjf #### Nginx安装 apache、nginx等是叫做web服务应用软件,其实就是web服务端程序,也叫做web服务器软件,英文为web server。 apache软件改了个名字,叫做httpd变成了现在apache金基会里面的一个软件。 https://mirrors.tuna.tsinghua.edu.cn/ ```sh # 可以改一下主机名,方便我们记忆:hostnamectl set-hostname webo1 安装方法1: 使用epel源安装 # yum repolist #查看当前系统的yum仓库有哪些软件包 yum install epel-release-y #安装yum的扩展包 yum install nginx -y systemctl start nginx.service 或者 systemctl start nginx systemctl enable nginx.service # netstat -lntup #查看端口占用情况 # 可以看到nginx默认占用了80端口 # 安装完之后,如果我们继续安装一个apache的httpd,还是可以安装上的 # yum instal1 httpd -y # 但是当我们启动httpd的时候会报错 # systemctl start httpd # 查看它的状态 # systemctl status httpd #可以看到启动失败了 ``` CentOS 7/8/9(包括 TencentOS、Alibaba Cloud Linux 等衍生版)的 Apache 主程序 `httpd` 来源于 **RPM 包** `httpd`,安装后文件主要分布在以下几个目录: 表格 复制 | 路径 | 作用 | | :--------------------------- | :-------------------------- | | `/usr/sbin/httpd` | 可执行二进制文件 | | `/etc/httpd/` | Apache 配置根目录 | | `/etc/httpd/conf/httpd.conf` | 主配置文件 | | `/etc/httpd/conf.d/*.conf` | 按模块/站点拆分的子配置文件 | | `/var/www/html/` | 默认网页根目录 | | `/var/log/httpd/` | 访问日志和错误日志 | #### 配置文件 ```sh ll /etc/nginx/ # 目录文件 conf.d default.d fastcgi.conf fastcgi.conf.default #fastcgi.conf的备份文件 fastcgi_params fastcgi_params.default #fastcgi_params的备份文件 koi-utf koi-win mime.types mime.types.default #mime.types的备份文件 nginx.conf #nginx的主配置文件,nginx每次启动都会加载它。 nginx.conf.default #nginx.conf的备份文件 scgi_params scgi_params.default #scgi_params的备份文件 uwsgi_params uwsgi_params.defau1t #uwsgi_params的备份文件 win-utf ``` 更新 ```sh #先过滤一下配置文件,因为里面的#号,空行等太多了,带#号的都是注释不用的,所以可以去掉 cd /etc/nginx/ grep -Ev '#|^$' nginx.conf.default > nginx.conf #编辑配置文件 vim nginx.conf #删除17-20行,剩下的就是最小配置了 #注意配置文件的语法格式,每行结尾必须是英文的分号。 worker_processes 1; # inx时工作进程的数量,可以加大这个数字来提高nginx的处理请求的效率,但是这个数字也不能太大,因为进程是消耗系统内存资源的。调整一下这个数字,然后通过free指令可以查看一下内存容量的变化。建议和cPU核数一致就行 #worker_processes2;#改完配置文件都需要重启nginx才生效,systemct1restartnginx.service events { worker_connections 1024; #连接数量,每个进程可以处理1024连接 } http { #http模块 include mime.types; #include是包含的意思,这行的意思是,nginx启动的时候加载nginx.conf主配置文件的时候,加载到这一行的时候,先包含加载一下mime.types文件里面的配置,这个文件主要是用来标识支持哪些多媒体格式,这个文件在nginx.conf所在目录 default_type application/octet-stream; # 能识别的文件,那么默认以八进制数据流的方式来打开文件 #下面这两个配置也可以删掉,现在不太适合讲,后面再讲 # sendfile on; # ke epalive_timeout 65; charset utf-8; #设置字符集,这是我多加的一个配置,默认没有,比如vimjaden.html写入一些中文,去掉和加上这个配置看看效果 server { # 一个网站,一个nginx可以运行事个网站,添加这个配置项即可 listen 80; #监听端口,可以修改,比如改个81看看效果,由启动apache看看80效果 server_name localhost; #网站的域名,现在没有配置域名,默认就是localhost,比如后面可以配置 location / { #目录,必须要有个1ocation root html; #root是站点根月录的意思,值为html表示一个相对路径,绝对路径是/usr/share/nginx/html,也可以改,比如改成绝对路径root/usr/share/nginx/html,或者改为其他路径root/web index index.html index.htm; #默认首页,访问网止根路径的时候,白动访问站点根口录下面的index或者index.html或名index.htm文件,如果没有这几个名字的文件呢?访问的时候就会提示403,需要在网址上手动指定文件名称,这几个文件名称也是可以改的,比如改为jaden.html. } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } ``` #### Http协议简要说明 #### include加载配置文件 将每个server放到conf.d目录下,在nginx.conf下配置 ```nginx http { include mime.types; default_type application/octet-stream; charset utf-8; include /etc/nginx/conf.d/*.conf } ``` 每个server命名就为该域名,例如:bjs.com.conf ```nginx server { listen 80; server_name c.bjs.com; location / { root /web/three; index index.html index.htm; } } ``` #### nginx日志 现在web服务器都必须要开启日志记录功能,而且记录必须超过半年,这是网络安全法规定的。https://www.wangan.com/wenda/6791 之前我们也看过日志,系统的安全日志,就是ssh登录的时候我们看的,如下: **cat /var/log/secure** 。 登录系统就会被记录。 nginx默认已经帮我们记录了日志,在/var/log/nginx/目录下面。 ```shell ls /var/log/nginx/ # 我们清空一下日志 > access.log cat access.log # 错误日志 cat error.log #没有favicon.ico文件,可以切换到站点目录(/web/three)中去下,载一个:wget https://www.xx.com/favicon.ico #还有人故意访问一个错误的路径,让你的网站报错,显示出nginx的版本。 # 解决:把错误页面换成自定义页面,或者在 nginx.conf 顶层 http 块里加一行 server_tokens off; # 隐藏版本号 error_log /opt/nginx_error.log info; # 定制日志记录格式:这个必须配置在在server配置外面 log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; #compression 可以理解为是这个格式的名字,谁想用这个格式,谁就用这个名字来指定格式 # $remote_addr 客户端的ip地址 # $remote_user 客户端的用户名 # $time_local 当前时间 # $request 请求起始行 # $status http状态码 # $bytes_sent 响应资源的大小 # $http_referer 记录资源的跳转地址 # $http_user_agent 用户的终端信息 # $gzip_ratiog zip的压缩级别 # 比如我们想让日志记录一下请求时间、客户端ip、请求uri、状态码、文件大小 # vim/etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { log_format test '[$time_local] $remote_addr "$request" $status $bytes_sent'; include mime.types; default_type application/octet-stream; charset utf-8; include /etc/nginx/conf.d/*.conf; } #每个网站都可以单独记录自己的日志 vim conf.d/bjs.com.conf server { listen 80; server_name c.bjs.com; access_log /opt/nginx/bjs.com_log test;#test是上面指定的日志格式的名称,/opt/目录下面没有nginx目录,需要我们手动创建,这个自录是随意指定的昂,mkdir/opt/nginx,还要授权:chown nginx:nginx/opt/nginx,不然nginx用户没办法访问这个目录 location / { root /web/three; index index.html index.htm; } } # 改完之后,重启nginx,然后访问网站,看一下/opt/nginx目录,看看日志格式。 # access_log /var/log/nginx/access.log compression; # access_log /opt/nginx/access.1ogcompression; # /opt/目录需要授权,不然没办法记录进去 ``` #### 开启basic认证 basic认证叫做http基本认证,就是给我们的网站多一把锁,防止恶意访问,比如访问一些敏感后台路径等操作。 首先生成一个叫做htpasswd的账号密码文件,有很多在线网站就能生成,如下:http://www.jsons.cn/htpasswd/,一般使用Crypt加密算法, 把生成的密码保存下来,比如保存到/etc/nginx/htpasswd文件中 ```nginx vim /etc/nginx/htpasswd #写入刚才保存的用户和密码 eix:YjMFB12fS2M1c ``` 然后修改一下nginx下的bjs网站的配置文件: ```nginx auth_basic "bjs.com"; #auth_basic表示开启这个功能,"bjs.com"是备注信息,随便写,一些老浏览器能看到,新浏览器都看不到备注信息了。 auth_basic_user_file /etc/nginx/htpasswd; #这是账号密码存放在哪个位置 # 存放位置在location中的 location / { auth_basic "bjs.com"; auth_basic_user_file /etc/nginx/htpasswd; ... } ``` 特殊路径开启认证权限: 方式一:正则 + 单 location(最简洁) ```nginx # 1. 需要 Basic Auth 的接口集合 location ~ ^/api/(user/edit|user/delete|order/cancel|admin/.*)$ { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd; proxy_pass http://127.2.3.4:5067$request_uri; proxy_set_header Host $host; } # 2. 其余 /api 接口一律直通 location /api/ { proxy_pass http://127.2.3.4:5067/; proxy_set_header Host $host; } ``` 说明 - 正则里用 `|` 分隔即可无限追加。 - 次序:`~` 正则优先级高于 `/api/` 前缀,所以不会冲突。 - 需要区分大小写就保持 `~`,想忽略大小写改成 `~*`。 - `admin/.*` 里的 `.*` 是“0 个或多个任意字符”,等价于“/api/admin/ 后面无论再跟多少层路径”都算命中。 举例: ```nginx location ^~ /agent/admin/ { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/blog.htpasswd; proxy_pass http://56.26.34.12:8052; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; } location /agent { proxy_pass http://56.26.34.12:8052/agent; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; } ``` 方式二:单独映射文件(可读性最高) 1. 创建白名单文件 `/etc/nginx/auth_uri.lst`(一行一个 URI): ``` /api/user/edit /api/user/delete /api/order/cancel ... ``` 1. nginx.conf: ```nginx http { map $request_uri $need_auth { default 0; include /etc/nginx/auth_uri.lst; # 读取白名单 } server { listen 80; server_name example.com; location /api/ { if ($need_auth = 1) { # 触发 auth error_page 401 = @auth; return 401; } proxy_pass http://127.2.3.4:5067/; proxy_set_header Host $host; } location @auth { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd; proxy_pass http://127.2.3.4:5067$request_uri; proxy_set_header Host $host; } } } ``` 优点: • 修改名单只需改文本文件,nginx -s reload 即可,无需改主配置。 • 白名单可以交给运维或 CI 自动生成。 #### ssl证书和https网站部署 #### Return网址跳转/Rewrite网址跳转 #### Gzip压缩 #### Nginx目录浏览功能 #### Nginx站点访问控制黑白名单 #### Location访问路径控制 #### Nginx常用变量 #### Referer放盗链示例 #### Nginx部署php动态网站 #### 网盘项目的反弹shell漏洞演示