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存放路径,日志存放路径,配置文件引入,允许生成worker process等

  events   配置影响nginx服务器与用户的网络连接,每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等

  http     可以嵌套多个server 配置代理、缓存、日志定义等绝大多数功能和第三方模块的配置

  server    配置虚拟主机的相关参数,一个httpd中可以有多个sercer

  location  配置请求的路由,以及各种页面的处理情况

[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 {
   autoindex on;           #添加此行,访问目录下没有索引文件,则显示目录下所有内容 include mime.types;    #文件扩展名与文件类型映射表(引用文件) default_type application
/octet-stream;    #默认应用类型
   server_tokens off;        #添加此行,隐藏版本号 #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
posted @ 2020-12-06 18:15  心恩惠动  阅读(208)  评论(0编辑  收藏  举报