浅谈nginx服务代理
07 Apr 2025nginx
用docker
安装
nginx
服务安装,我是用docker
安装的,mac
系统编译nginx
源码安装有点问题。
docker pull nginx # 从docker仓库拉取nginx镜像
# nginx目录挂载参考此文章,https://blog.csdn.net/baidu_21349635/article/details/102738972
docker run --name nginx-0807 -v /Users/madong/software/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/madong/software/nginx/conf.d:/etc/nginx/conf.d \
-v /Users/madong/software/nginx/html:/usr/share/nginx/html \
-v /Users/madong/software/nginx/logs:/var/log/nginx -p 8080:80 -d nginx
# nginx服务验证,curl有返回html内容时,则表示nginx服务启动成功了; /etc/nginx存nginx配置、/usr/share/nginx存在html、/var/log/nginx/存放nginx的access_log
curl 'http://localhost:8080/'
nginx
也支持热更新,当修改nginx配置后,可使用nginx -s reload
使修改的配置生效,当access.log
文件特别大时,可使用nginx -s reopen
切割日志。
nginx
用源码安装
使用nginx
源码包安装,用docker nginx
有个明显的问题,那就是nginx.conf
中listen
不同端口时,容器内端口向外映射很麻烦,所以用源代码装。
步骤可分为以下4
步:
- 从
nginx
官网下载stable version
源码,https://nginx.org/en/download.html; - 由于
nginx
有c
的代码,所以需下载pcre
、openssl
和zlib
工具,具体可参考文章:https://blog.csdn.net/a1004084857/article/details/128512612; - 解压
nginx zip
包,进入解压路径,对nginx
进行配置,--prefix
指定编译后的nginx
二进制文件存放目录,--with-pcre|zlib|openssl
分别为工具的解压逻辑;./configure --prefix=/Users/madong/software/c_nginx_1.28.0 \ --with-http_ssl_module \ --with-pcre=./compile/pcre-8.45 \ --with-zlib=./compile/zlib-1.3.1 \ --with-openssl=./compile//openssl-3.0.7
- 配置完成后,执行
make install
,则编译后的nginx
就会出现在/Users/madong/software/c_nginx_1.28.0
这个目录中;
nginx
搭建一个静态资源web服务器
nginx
源码编译后,会生成conf
、html
、logs
、sbin
这几个目录,其分别是:nginx
配置、html
默认页面、access_log
、启动脚本等,启动nginx
的脚本:./sbin/nginx
。
下方的内容是nginx.cnf
配置的一部分,在nginx
的安装目录下放了neo4j
文档,在线文档对应路径为neo4j
。配置解释,nginx
监听8093
端口,location /
表示请求url为ip:8093:/
时,进入此配置代码块。
server {
listen 8093;
location / {
# autoindex on;
alias neo4j/;
# set $limit_rate 1k; #限制nginx向浏览器发送流量的速度
# root html;
# index nothing; # 禁用inde文件
index index.html index.htm;
}
}
代码配置中有root
和alias
两个指令,建议用alias
(一般用root
配根目录,用alias
配置一般的路径),打开浏览器,输入http://127.0.0.1:8093/
就可以访问到静态资源文档,如果想限制浏览器下载资源速度,可设置set $limit_rate 1k;
。

若想将文件目录设置成资源服务器,在配置中打开autoindex on;
和index nothing;
,需注释index index.html index.htm;
,因为即使配置了目录检索,当目录下存在index.html
时,默认也是打开index.html
。

提升浏览器获取静态资源的速度,在nginx.conf
中打开gzip on;
,同时也可指定gzip_min_length
、gzip_comp_level
、gzip_types
内容,对静态资源使用gzip
进行压缩。
GoAccess
实时监控nginx
访问
在nginx
的logs
目录中有请求访问的日志,在location
中的配置,首先log_format
定义了访问日志的格式,在server
中定义了access_log
的路径以及应用的格式。
# nginx log日志的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
# ...
access_log logs/neo4jdoc.access.log main;
}
从GoAccess
网站下载安装包,很奇怪,C
相关的应用下载都是源码包,没有针对于特定系统的二进制包,还需安装依赖包libmaxminddb
,否则安装时会报错** Missing development files for libmaxminddb library
。
从https://github.com/maxmind/libmaxminddb
下载安装包,在本地环境解压,进入到libmaxminddb
目录,执行./configure
然后执行make install
进行安装。然后进入GoAccess
安装包的解压目录,执行:./configure --enable-utf8 --enable-geoip=mmdb
、make install
指令进行安装。
进入nginx
的logs
目录,执行实时监控命令,生成report.html
,同时在nginx.conf
配置report.html
的路由,以便在浏览器中访问监控页面。
LANG="en_US.UTF-8" LC_TIME="en_US.UTF-8" bash -c 'goaccess neo4jdoc.access.log -o ../html/report.html --real-time-html --log-format=COMBINED'
server {
#... 对外暴露report.html页面
location /report.html {
alias /Users/madong/software/c_nginx_1.28.0/html/report.html;
}
}
在浏览器中输入http://127.0.0.1:8093/report.html
即可以看到监控页面,有一点,在html
页面上一开始可能会展示unauthorized
,等过一会儿web socket
就能建立成功,就可以展示页面上的指标,例如:请求命中数、请求文件url
统计、静态请求数、404
的url
统计等。

