Nginx目录结构与配置文件详解

Nginx安装

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

安装依赖

安装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
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support
is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 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;

}

}

[【华为云-精选30+云服务产品助力好友上云】](https://activity.huaweicloud.com/1212_promotion/index.html?fromacct=c76cea9f-f106-4938-a195-0df9b712c327&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905adp=1.3.1.1&adc=2.2) ![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/9c6d7e36ee7a0f9ed3c685b1d634a6c7.png)
posted @ 2019-12-13 09:24  避凉闲庭  阅读(582)  评论(0编辑  收藏  举报