首页 > 系统相关 >Nginx--安装&&配置文件

Nginx--安装&&配置文件

时间:2024-02-05 11:05:39浏览次数:27  
标签:index http 配置文件 nginx -- Nginx html location root

官网: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配置文件主要分为五大块

日志存放路径,配置文件引入,允许生成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如何解释location后面的uri

  alias是一个目录别名的定义,root则是最上层目录的定义

使用alias时,目录名后面一定要加”/“

root路径+location路径

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



标签:index,http,配置文件,nginx,--,Nginx,html,location,root
From: https://blog.51cto.com/u_16558404/9601581

相关文章

  • Nginx--安装模块
    一 安装系统自带模块#进入安装目录[root@localhost~]#cdnginx-1.18.0/#查看原来的编译选项[[email protected]]#nginx-V#查看安装的模块[[email protected]]#./configure--help#以http_v2_module为例[[email protected]]#./confi......
  • Nginx--调优
    1 Nginx运行工作进程数量(worker_processes)Nginx运行工作进程个数一般设置CPU的核心数或者auto,最多8个,8个以上性能提升不会再提升了,而且稳定性变得更低 2 Nginx运行CPU亲和力(worker_cpu_affinity)(全局快添加)nginx默认是没有开启利用多核cpu的配置的,需要通过增加worker_......
  • Nginx--用户认证&&访问控制&&限速&&状态访问
    一 用户认证某些网页只希望给特定的用户访问,可以设置用户认证,使用户访问时需要进行身份认证,只有认证通过才可访问网页location/{roothtml;indexindex.htmlindex.htm;auth_basic"haha";#服务器描述......
  • Nginx--虚拟主机
    一 基于域名server{listen80;server_namewww.google.com;location/{roothtml/google;indexindex.htmlindex.htm;}}server{listen80default;#default在浏览器中直接输入IP地址会进入这个......
  • Nginx--缓存
    一般存的是静态资源,可以提高客户端的访问速度,并减轻服务器的压力 1 客户端缓存通过设置expires指令,响应头中将会返回Expires和Cache-Control字段当浏览器发现响应头存在这样的缓存字段,当再次请求相同资源时,就会确认在客户端的资源是否过期location/{expires30m......
  • Nginx--rewrite
    rewriteURL重写,可以在改变网站结构后,无需要求客户端用户修改原有的浏览器书签,也无需其他网站修改对我们网站的友情链接,依赖于PCRE库的支持 1if语法:if(condition){...}默认值:—上下文:server,location如果condition为真,执行定义在大括号中的rewr......
  • Nginx--引用多配置文件
    在nginx.conf的http模块,include指定某个目录下的*.confusernginx;worker_processesauto;error_log/var/log/nginx/error.log;pid/run/nginx.pid;#Loaddynamicmodules.See/usr/share/nginx/README.dynamic.include/usr/share/nginx/modules/*.conf;events{......
  • Nginx--平滑升级
    在不中断服务的情况下,新的请求也不会丢失,使用新的nginx可执行程序替换旧的 1 查看老版本的编译选项[root@localhost~]#nginx-Vnginxversion:nginx/1.16.0builtbygcc4.8.520150623(RedHat4.8.5-44)(GCC)builtwithOpenSSL1.0.2k-fips26Jan2017TLSSNI......
  • Nginx--upstream健康检查
    nginx判断节点失效状态:Nginx默认判断失败节点状态以connectrefuse和timeout状态为准,不以HTTP错误状态进行判断失败,因为HTTP只要能返回状态说明该节点还可以正常连接,所以nginx判断其还是存活状态;除非添加了proxy_next_upstream指令设置对404、502、503、504、500和timeout......
  • DNS--安装&&配置文件
    1 下载#下载服务yum-yinstallbind#下载解析工具yum-yinstallbind-utils 2 配置文件主配置文件/etc/named.conf区配置文件/var/named/配置文件模板/usr/share/doc/bind-9.8.2/sample 3 协议及端口TCP53主要用于主从同步,需要的是可靠的数......