nginx目录结构和命令:(编译安装的)
[root@centos7b nginx-1.14.1]# tree /etc/nginx/
/etc/nginx/
├── fastcgi.conf-------------------->>>#fastcgi相关参数的配置文件
├── fastcgi.conf.default------------>>>#fastcgi.conf的原始备份
├── fastcgi_params------------------>>>#fastcgi的参数文件
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types---------------------->>>#媒体类型
├── mime.types.default
├── nginx.conf---------------------->>>#这是Nginx默认的主配置文件
├── nginx.conf.default
├── scgi_params--------------------->>>#scgi相关参数文件,一般用不到
├── scgi_params.default
├── uwsgi_params-------------------->>>#uwsgi相关参数文件,一般用不到
├── uwsgi_params.default
└── win-utf
[root@centos7b nginx-1.14.1]# tree /usr/local/nginx/
/usr/local/nginx/
├── html-------------------->>>#这是编译安装时Nginx的默认站点目录,类似Apache的默认站点htdocs目录
│ ├── 50x.html-------------------->>>#错误页面优雅替代显示文件,例如:出现502错误时会调用此页面
│ └── index.html-------------------->>>#默认的首页文件,首页文件名字是在nginx.conf中事先定义好的
└── sbin
└── nginx
[root@centos7b nginx]# tree /var/log/nginx/
/var/log/nginx/-------------------->>>#这是Nginx默认的日志路径,包括错误日志及访问日志
├── access.log
└── error.log
nginx常用命令:
nginx:默认为启动nginx,不能用systemd关闭,但systemd开启后可以用nginx默认启动
-h 查看帮助选项
-V 查看版本和配置选项
-t 测试nginx语法错误
-c filename 指定配置文件(default: /etc/nginx/nginx.conf)
-s signal 发送信号给master进程,signal:stop, quit, reopen, reload
示例: nginx -s stop 停止nginx
nginx -s reload 加载配置文件
-g directives 在命令行中指明全局指令
nginx配置:
main block:全局主配置
正常运行:
性能优化:
事件驱动:event {...}
调试定位:
http{..}
ngx_http_core_module--------------->>>#包括一些核心的http参数设置,对应Nginx的配置为HTTP区块部分
ngx_http_access_module------------->>>#可实现基于ip的访问控制功能
ngx_http_auth_basic_module--------->>>#实现基于用户的访问控制,使用basic机制进行用户认证
ngx_http_stub_status_module-------->>>#用于输出nginx的基本状态信息
ngx_http_log_module---------------->>>#指定日志格式记录请求
ngx_http_gzip_module--------------->>>#压缩模块,对nginx返回的数据压缩,属于性能优化模块,节约带宽
ngx_http_ssl_module---------------->>>#ssl模块,用于加密的http连接,如https
ngx_http_rewrite_module------------>>>#将用户请求的URI进行检查,而后完成重定向替换
ngx_http_referer_module------------>>>#用来组织Referer首部无有效值的请求访问,可防止盗链
ngx_http_proxy_module-------------->>>#转发请求至另一台主机
ngx_http_headers_module------------>>>#通过代理服务器给客户端的响应报文添加自定义首部,或修改指定首部的值
ngx_http_fastcgi_module------------>>>#转发请求到FastCGI服务器,不支持php模块方式
ngx_http_upstream_module----------->>>#付费版,负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
mail{..}------------------------------->>>#生产中用的很少,不深究
stream{..}四层代理配置段
ngx_stream_core_module------------->>>#模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器
ngx_stream_proxy_module------------>>>#可实现代理基于TCP,UDP (1.9.13), UNIX-domain sockets的数据流
下面重点说下http协议相关的配置结构:
http {
...-------------------->>>各server的公共配置,对所有server都生效
...
server {
...-------------------->>>每个server用于定义一个虚拟主机
}
#server里面的内容:
server {
listen -------------------->>>监听端口
server_name-------------------->>>服务名
root -------------------->>>站点根目录
alias-------------------->>>别名
location [OPERATOR] URL {-------------------->>>对某一个url访问的资源属性
if CONDITION {
...
}
}
}
}
查看nginx默认配置文件
[root@www conf]# egrep -v "#|^$" nginx.conf.default------------->>>#去掉包含#号和空行的内容
worker_processes 1;------------->>># worker进程的数量
events { ------------->>># 事件区块开始
worker_connections 1024; ------------->>>#每个worker进程支持的最大连接数
} ------------->>>#事件区块结束
http { ------------->>>#HTTP区块开始
include mime.types; ------------->>>#Nginx支持的媒体类型库文件
default_type application/octet-stream; ------------->>>#默认的媒体类型
sendfile on; ------------->>>#开启高效传输模式
keepalive_timeout 65; ------------->>>#连接超时
server { ------------->>>#第一个Server区块开始,表示一个独立的虚拟主机站点
listen 80; ------------->>>#提供服务的端口,默认80
server_name localhost; ------------->>>#提供服务的域名主机名
location / { ------------->>>#第一个location区块开始
root html; ------------->>>#站点的根目录,相当于Nginx的安装目录
index index.html index.htm;------------->>>#默认的首页文件,多个用空格分开
} ←第一个location区块结果
error_page 500 502 503 504 /50x.html;------------->>>#出现对应的http状态码时,使用50x.html回应客户
location = /50x.html { ------------->>>#location区块开始,访问50x.html
root html; ------------->>>#指定对应的站点目录为html
}
}
}
nginx配置模块功能大全
user nginx nginx; ------------->>>#定义Nginx运行的用户和用户组
worker_processes 1; ------------>>>nginx进程数,建议设置为等于CPU总核心数。
error_log /var/log/nginx/error.log info;-------------------->>>全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
pid /var/run/nginx.pid; -------------------->>>进程文件
include /usr/share/nginx/modules/*.conf;
worker_rlimit_nofile 1024;-------------------->>>一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以#建议与ulimit -n的值保持一致
events
{
use epoll; -------------------->>>参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,
如果跑在FreeBS #D上面,就用kqueue模型。
worker_connections 65535;-------------------->>>单个进程最大连接数(最大连接数=连接数*进程数)
}
http -------------------->>>HTTP区块开始
{
include mime.types; #Nginx支持的媒体类型库文件
default_type application/octet-stream; -------------------->>>默认媒体类型
#charset utf-8; -------------------->>>默认编码
server_names_hash_bucket_size 128; -------------------->>>服务器名字的hash表大小
client_header_buffer_size 32k; -------------------->>>上传文件大小限制
large_client_header_buffers 4 64k; -------------------->>>设定请求缓
client_max_body_size 8m; -------------------->>>设定请求缓
sendfile on; -------------------->>>开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为o #ff,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
autoindex on; -------------------->>>开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on; -------------------->>>防止网络阻塞
tcp_nodelay on; -------------------->>>防止网络阻塞
keepalive_timeout 120; -------------------->>>连接超时,单位是秒
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzip模块设置
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; -------------------->>>v压缩等级
gzip_types text/x-javascript text/css application/xml;-------------------->>>压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addrv 10m;-------------------->>>开启限制IP连接数的时候需要使用
#虚拟主机的配置
server
{
listen 80; ------------->>>监听端口
server_name localhost; ------------->>>提供服务的域名主机名
location / { ------------->>>#第一个location区块开始
root html; -------------------->>>站点的根目录,相当于Nginx的安装目录
index index.html index.htm index.jsp;---------->>>#默认的首页文件,多个用空格分开
} ------------->>>#第一个location区块结果
#图片缓存时间设置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS和CSS缓存时间设置
location ~ .*\.(js|css)?$
{
expires 1h;
}
#日志格式设定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/log/nginx/access_$(data+%F -d -1day).log access;-------------------->>>定义本虚拟主机的访问日志
location / { -------------------->>>#对 "/" 启用反向代理
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr; -------------------->>>后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
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服务器传
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd; -------------------->>>htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}
#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
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或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}
nginx+httpd+php-fpm+mysql动静分离:
client:
IP:192.168.71.6
nginx:
IP:192.168.71.7;172.20.222.2
server {
listen 80 default_server;---->需要在默认主配置文件中将default_server删除
server_name www.a.com;---->任意指定的
root /data/sitea/;---->任意指定的
access_log /var/log/nginx/a_access.log main;
location ~ \.php$ {
proxy_pass http://192.168.71.17;
}
location / {
proxy_pass http://192.168.71.200;
}
}
http:
IP:192.168.71.200;systemctl start httpd;echo httpd > /var/www/html/index.html
php-fpm:
IP:192.168.71.17
vim /etc/httpd/conf.d/php.conf
DirectoryIndex index.php
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
vim /var/www/html/index.php;
<?php
phpinfo();
?>
vim /var/www/html/test.php;
<?php
$dsn='mysql;host=192.168.71.27;dbname=mysql';
username='test';
$passwd='centos';
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
?>
mysql:
IP:192.168.71.27;
systemctl start mariadb;
mysql;grant all on *.* to test@'192.168.71.%'identified by 'centos';
转载请注明:黑夜 » nginx系列之(四)配置及常见模块