Nginx--安装&&配置文件
官网:http://nginx.org/en/download.html
nginx版本:1.18
一 安装
1 下载预编译环境(预编译报错需要安装什么库 直接在库名后面接 -devel 使用yum下载)
yum -y install pcre-devel openssl-devel gcc gcc-c++
2 创建用户,解压包
useradd -M -r -s /sbin/nologin nginx
tar xvf nginx-1.18.0.tar.gz
3 预编译
[root@localhost ~]# cd nginx-1.18.0/ [root@localhost nginx-1.18.0]# ./configure --with-http_realip_module --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
编译选项
--prefix=path | 指定安装路径,默认/usr/local/ |
--sbin-path=path | 指定二进制命令的路径 |
--conf-path=path
|
指定配置文件conf路径 |
--error-log-path= | 指定错误日志error.log路径,默认/usr/local/nginx/logs/ |
--http-log-path= | 指定主日志access.log路径 |
--with-http_stub_status_module | 启用service status页,默认不启用 |
--with-http_ssl_module | 启用ssl模块,以支持https请求 |
--with-http_realip_module | 获取客户端真实ip时使用 |
4 编译安装
make && make install
5 命令
/usr/local/nginx/sbin/nginx -s | 给主进程发送信号.可接 stop | quit | reopen | reload(不关闭服务重置配置文件) |
/usr/local/nginx/sbin/nginx -t | 检查主配置文件有无错误 |
/usr/local/nginx/sbin/nginx -v | 查看版本号 |
/usr/local/nginx/sbin/nginx -V | 查看版本号及编译选项 |
/usr/local/nginx/sbin/nginx -c | 指定配置文件,默认为 conf/nginx.conf |
#设置命令 ln -s /usr/local/nginx/sbin/* /usr/local/sbin
二 配置文件详解(/usr/local/nginx/conf/nginx.conf)
nginx配置文件主要分为五大块
全局块(main) 配置影响nginx全局的指令,一般有nginx服务器的用户组,nginx进程pid存放路径,
[root@localhost nginx]# cat /usr/local/nginx/conf/nginx.conf #user nobody; #nginx用户及组,如果用户和组名一样可只写一个,一般写作 user nginx; worker_processes 1; #工作进程的数量,不要超过8,一般为cpu核数,不确定时设为auto(自动检测)最优值取决很多因素,包括但不限于CPU核的数量,存储数据的硬盘,数量及负载模式 #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; #定义日志文件(可以用include 路径/文件名) sendfile on; #开启高效文件传输模式 #tcp_nopush on; #必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量(将响应头和正文的开始部分一起发送,而不一个接一个的发送。) #keepalive_timeout 0; keepalive_timeout 65; #连接超时时间,长连接,一般开启 #gzip on; #压缩传输 server { #http子块 每一个server都是一台虚拟主机 listen 80; #监听端口 server_name localhost; #虚拟主机域名
client_max_body_size 10M; #限制上传文件大小
#charset koi8-r; #字符集 #access_log logs/host.access.log main; #虚拟主机级别日志 location / { #url浏览器输入的路径
root html; #访问的目录/usr/local/nginx/html index index.html index.htm; #索引文件 } #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; # } #} }
三 location
语法 location [修饰符] /uri|pattern {...}
修饰符 =
^~
~ 正则匹配,区分大小写
~* 正则匹配,不区分大小写
没有修饰符,优先级最低
注:
优先级的高低与location出现顺序无关
尽量使用单一的location修饰符去完成任务
如果uri为前缀目录,动作为proxy_pass 代理的IP后面有目录,前缀目录会被覆盖掉
root,alias指令
nginx指定文件路径有两种方式:root,alias,主要区别在于nginx如何解释location后面的uri
alias是一个目录别名的定义,root则是最上层目录的定义
使用alias时,目录名后面一定要加”/“
root处理结果:root路径+location路径
alias处理结果:alias路径
root:
语法: root path
默认值:root html
配置段:http server location
alias:
语法: alias path
配置段:location
例:
#访问uri:/test,实际访问的是 /tmp/html/test/index.html 这个文件
location /test { root /tmp/html; index index.html; }
#访问uri:/test,实际访问的是 /tmp/html/index.html 这个文件 location /test { alias /tmp/html/; index index.html; }
四 日志(内嵌变量)
更多内嵌变量参见:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables
$remote_addr | 记录直接与服务器通信的客户端的IP |
$remote_user | 记录远程客户端的名字 |
$time_local | 记录访问时间 |
$request | 记录请求的URL(资源定位) |
$status | 记录请求状态 |
$body_bytes_sent | 记录发送给客户端的文件的内容的大小 |
$http_referer | 记录从哪个页面链接访问过来的 |
$http_user_agent | 记录客户端浏览器的信息 |
$http_x_forwarded_for | 记录从请求报文首部的X-Forwarded-For字段获取的值 |
$http_x_real_ip | 记录从请求报文首部的X-Real-IP字段获取值 |
$realip_remote_addr | 最后一个反向代理服务器的IP |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具