新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Nginx目录结构与配置文件详解-创新互联

Nginx安装

具体安装nginx请移步:nginx部署

创新互联是一家专业提供民和企业网站建设,专注与成都网站设计、成都做网站H5网站设计、小程序制作等业务。10年已为民和众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

安装依赖

安装pcre依赖软件
[root@ubuntu ~]# yum install -y pcre pcre-devel         //外网情况使用此安装方式
[root@ubuntu ~]# rpm -ivh pcre      //内网情况使用此安装方式
[root@ubuntu ~]# rpm -ivh pcre-devel        //内网情况使用此安装方式
#编译安装pcre依赖软件(注意编译安装软件包需要开发工具的支持安装nginx需要make、gcc、c++支持)
#下载地址: https://ftp.pcre.org/pub/pcre/       //根据相关情况进行选择版本下载
[root@ubuntu ~]# wget https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.gz        //下载pcre软件包
[root@ubuntu ~]# tar zxf pcre-7.80.tar.gz       //解压软件包
[root@ubuntu ~]# cd pcre-7.80       //进入相关目录
[root@ubuntu ~]# ./configure        //配置
[root@ubuntu ~]# make && make install       //编译并编译部署
安装openssl依赖软件
[root@ubuntu ~]# yum install -y openssl openssl-devel       //外网情况使用此安装方式
[root@ubuntu ~]# rpm -ivh openssl       //内网情况使用此安装方式
[root@ubuntu ~]# rpm -ivh openssl-devel         //内网情况使用此安装方式
#编译安装与pcre几乎一致,请参考上一小节

安装Nginx

nginx下载地址:nginx官网地址,下载nginx时需要注意版本问题,nginx分为三个版本:稳定版、开发版和老版本稳定版

检查依赖是否安装
[root@ubuntu ~]# rpm -qa pcre-devel pcre
 pcre-devel-7.8-6.el6.x86_64       #pcre的devel字符串包已经安装
 pcre-7.8-6.el6.x86_64             #pcre软件包已经安装
[root@ubuntu ~]# rpm -qa openssl-devel openssl
 openssl-1.0.1e-30.el6.x86_64        #openssl的包已经安装
 openssl-devel-1.1.13-30.el6.x86_64     #openssl的devel字符串包已经安装
安装nginx
[root@ubuntu:/opt]# wget -q http://nginx.org/download/nginx-1.17.6.tar.gz       //下载软件包
[root@ubuntu:/opt]# ls  nginx-1.17.6.tar.gz     //查看是否下载成功
-rw-r--r-- 1 root root 1037527 Nov 19 14:25 nginx-1.17.6.tar.gz
[root@ubuntu:/opt]# useradd nginx -s /sbin/nologin -M       //创建nginx执行用户
[root@ubuntu:/opt]# tar xf nginx-1.17.6.tar.gz      //解压nginx软件包
[root@ubuntu:/opt]# cd nginx-1.17.6     //进入解压目录
[root@ubuntu nginx-1.17.6]#./configure  --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_ssl_module     //配置nginx软件包

#具体配置模块讲解请移步nginx编译配置模块大全

[root@ubuntu nginx-1.17.6]# make            //编译nginx软件包
[root@ubuntu nginx-1.17.6]# make install        //部署nginx软件包

启动nginx检查是否安装成功

检查nginx语法
root@ubuntu:~# /usr/local/nginx/sbin/nginx -t       //查看nginx配置文件是否正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok     //表示语法正确
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful  //test成功
启动nginx
root@ubuntu:~# /usr/local/nginx/sbin/nginx      //直接启动就可以

具体的启动方式请移步:nginx部署

查看Nginx端口是否启动
root@ubuntu:~# lsof -i :80      //查看相关端口
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   23658   root    6u  IPv4  98258      0t0  TCP *:http (LISTEN)
nginx   23659 nobody    6u  IPv4  98258      0t0  TCP *:http (LISTEN)
root@ubuntu:~# netstat -anpl | grep nginx       //查看nginx的相关端口
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23658/nginx: master 
unix  3      [ ]         STREAM     CONNECTED     98314    23658/nginx: master  
unix  3      [ ]         STREAM     CONNECTED     98315    23658/nginx: master  
root@ubuntu:~#
访问nginx
root@ubuntu:~# curl 127.0.0.1       //访问nginx



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

nginx目录结构说明

root@ubuntu:~# tree /usr/local/nginx/
/usr/local/nginx/
├── client_body_temp
├── conf            #存放一系列配置文件的目录
│   ├── fastcgi.conf        #fastcgi程序相关配置文件
│   ├── fastcgi.conf.default    #fastcgi程序相关配置文件备份
│   ├── fastcgi_params      #fastcgi程序参数文件
│   ├── fastcgi_params.default      #fastcgi程序参数文件备份
│   ├── koi-utf     #编码映射文件
│   ├── koi-win     #编码映射文件
│   ├── mime.types      #媒体类型控制文件
│   ├── mime.types.default      #媒体类型控制文件备份
│   ├── nginx.conf      #主配置文件
│   ├── nginx.conf.default      #主配置文件备份
│   ├── scgi_params     #scgi程序相关配置文件
│   ├── scgi_params.default     #scgi程序相关配置文件备份
│   ├── uwsgi_params        #uwsgi程序相关配置文件
│   ├── uwsgi_params.default        #uwsgi程序相关配置文件备份
│   └── win-utf     #编码映射文件
├── fastcgi_temp        #存放fastcgi程序临时文件
├── html        #存放网页文档
│   ├── 50x.html        #错误页码显示网页文件
│   └── index.html      #网页的首页文件
├── logs        #存放nginx的日志文件
│   ├── access.log      #默认访问日志
│   ├── error.log       #错误日志
│   └── nginx.pid       #nginx pid文件
├── proxy_temp      #代理相关临时文件
├── sbin        #存放启动程序
│   └── nginx       #nginx启动程序
├── scgi_temp       #存放scgi程序临时文件
└── uwsgi_temp      #存放uwsgi程序临时文件

9 directories, 21 files

主配置文件说明

#user  nobody;      //定义执行nginx程序的用户
worker_processes  1;        #指定nginx进程数

#错误日志定义类型有:debug、info、notice、warn、error、crit
#error_log  logs/error.log;     //指定错误日志目录
#error_log  logs/error.log  notice;     //指定notice类型的错误日志目录
#error_log  logs/error.log  info;       //指定全局错误日志目录
#worker_rlimit_nofile 1024;         //指定但进程最多打开文件数量
#pid        logs/nginx.pid;     //指定进程文件的目录

events {            #事件区块
#事件模型有:kqueue、rtsig、epoll、/dev/poll、select、poll
    use epoll;      #指定参考事件模型
   worker_connections  1024;       #指定单进程大链接数
}

http {      #http区块
    include       mime.types;       #指定nginx支持的媒体类型
    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;     //指定nginx的访问日志目录

    sendfile        on;     #开启高效的传输模式
    #tcp_nopush     on;     //防止网络阻塞

    #keepalive_timeout  0;      //链接超时事件
    keepalive_timeout  65;

    #gzip  on;      //开启gzip压缩输出
    #gzip_min_length 1k;        //最小压缩文件大小
    #gzip_buffers 4 16k;        //压缩缓冲区
    #gzip_http_version 1.0;     //压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    #gzip_comp_level 2;     //压缩等级
    #gzip_types text/x-javascript text/css application/xml;     //压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    #gzip_vary on;      //增加响应头”Vary: Accept-Encoding”
    #limit_zone crawler $binary_remote_addr 10m;        //开启限制IP连接数的时候需要使用
    #FastCGI程序相关参数
    #配置FastCGI程序相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
    #fastcgi_connect_timeout 300;       //连接到后端fastcgi超时时间
    #fastcgi_send_timeout 300;      //向fastcgi请求超时时间(这个指定值已经完成两次握手后向fastcgi传送请求的超时时间)
    #fastcgi_read_timeout 300;      //接收fastcgi应答超时时间,同理也是2次握手后
    #fastcgi_buffer_size 64k;       //读取fastcgi应答第一部分需要多大缓冲区,该值表示使用1个64kb的缓冲区读取应答第一部分(应答头),可以设置为fastcgi_buffers选项缓冲区大小
    #fastcgi_buffers 4 64k;     //指定本地需要多少和多大的缓冲区来缓冲fastcgi应答请求,假设一个php或java脚本所产生页面大小为256kb,那么会为其分配4个64kb的缓冲来缓存;若页面大于256kb,那么大于的256kb的部分会缓存到fastcgi_temp指定路径中,这并非是个好办法,内存数据处理快于硬盘,一般该值应该为站点中php/java脚本所产生页面大小中间值,如果站点大部分脚本所产生的页面大小为256kb,那么可把值设置为16 16k,4 64k等
    #fastcgi_busy_buffers_size 128k;        //默认值是fastcgi_buffer的2倍
    #fastcgi_temp_file_write_size 128k;     //写入缓存文件使用多大的数据块,默认值是fastcgi_buffer的2倍
    #fastcgi_cache TEST;        //开启fastcgi缓存并为其指定为TEST名称,降低cpu负载,防止502错误发生.
#应答代码缓存时间,200和302应答缓存为1个小时,301一天,其他1分钟
    #fastcgi_cache_valid 200 302 1h;
    #fastcgi_cache_valid 301 1d;
    #fastcgi_cache_valid any 1m;
#主机配置
    server {        #server区块,每一个区块表示一个虚拟主机
        listen       80;        #侦听端口
        server_name  localhost;     #配置此主机的域名

        #charset koi8-r;        //配置字符编码

        #access_log  logs/host.access.log  main;        //配置默认访问日志目录

        location / {        #location区块 通过指定模式来与客户端请求的URI相匹配
            root   html;        #配置站点根目录
            index  index.html index.htm;        #默认文件
        }

        #error_page  404              /404.html;    //当发生404错误的时候能够显示一个预定义的uri

        # redirect server error pages to the static page /50x.html      //将服务器错误页重定向到静态页/50x.html
        #
        error_page   500 502 503 504  /50x.html;        #当发生其中错误的时候能够显示一个预定义的uri

#反向代理
        #location / {
            #proxy_pass http://127.0.0.1:88;    //指定代理地址
            #proxy_redirect off;        //当上游服务器返回的响应是重定向或刷新请求(如HTTP响应码是301或者302)时,proxy_redirect可以重设HTTP头部的location或refresh字段。可选参数default、off、redirect replacement]
    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            #proxy_set_header X-Real-IP $remote_addr;   
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #proxy_set_header Host $host;   //允许重新定义或者添加发往后端服务器的请求头
            #client_max_body_size 10m;      //允许客户端请求的大单文件字节数
            #client_body_buffer_size 128k;      //缓冲区代理缓冲用户端请求的大字节数
            #proxy_connect_timeout 90;      //nginx跟后端服务器连接超时时间(代理连接超时)
            #proxy_send_timeout 90;      //后端服务器数据回传时间(代理发送超时)
            #proxy_read_timeout 90;     //连接成功后,后端服务器响应时间(代理接收超时)
            #proxy_buffer_size 4k;      //设置代理服务器(nginx)保存用户头信息的缓冲区大小
            #proxy_buffers 4 32k;       //proxy_buffers缓冲区,网页平均在32k以下的设置
            #proxy_busy_buffers_size 64k;       //高负荷下缓冲大小(proxy_buffers*2)
            #proxy_temp_file_write_size 64k;        //设定缓存文件夹大小,大于这个值,将从upstream服务器传
106 
        #}

#设置js和css缓存时间
        #location ~ .*\.(js|css)?$
        #{
            #expires 2h;
        # }

#设置图片缓存事件
        #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        #{
            #expires 5d;
        # }

#监控Nginx状态的地址
    #location /NginxStatus {
        #stub_status on;        //开启监控状态
        #access_log on;         //开启默认访问监控
        #auth_basic "NginxStatus";      //设置验证模块
        #auth_basic_user_file conf/htpasswd;        //htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
    #}

#动静分离配置
#所有jsp的页面均交由tomcat处理
    #location ~ .(jsp|jspx|do)?$ {
        #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_pass http://127.0.0.1:8080;
    # }

#所有静态文件由nginx直接读取不经过tomcat
    #location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$#
    #{
        #expires 10d; 
    #}

    #location ~ .*.(js|css)?${
        #expires 2h;
    #}

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:Nginx目录结构与配置文件详解-创新互联
网站路径:http://www.wjwzjz.com/article/cceeej.html
在线咨询
服务热线
服务热线:028-86922220
TOP