写这篇博客的时候一直纠结文章的题目,本想只讲nginx,专门看了下nginx的源码分析,无奈难以一下吃透,顶多也是依样画葫芦般的写写nginx配置。但又不肯就此罢休,沦为一篇冗余的博文。思来想去就想结合一些应用来谈谈nginx配置,文章标题的由来正如此。如有雷同不当之处,还望指正,。现进入正题罢。。
linux添加开机启动有三种方式
/etc/rc.local
,在exit 0
之前加入需要开机执行的命令/etc/profile.d
目录下/etc/init.d
或/etc/rc.d/init.d
中;#!/bin/sh 告诉系统使用的shell
#chkconfig: 35 20 80 分别代表运行级别,启动优先权,关闭优先权 此行代码必须
#description: 文件描述随意发挥 但必须要有
(3)chkconfig –add 脚本文件名此处采用方式1,/etc/rc.local
内容如下:
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/etc/init.d/mysqld start
/etc/init.d/nginx start
/etc/init.d/php-fpm start
此处分别对应软件mysql,nginx,php,安装教程Google即可。
主配置文件/etc/nginx/nginx.confi如下:
user www www;
# 指定worker进程所属用户和组。
## 比如说一个静态网页服务器的文件目录的不同的用户有不同的访问权限,使用 nginx 指定用户就可以有权限对此目录读写。
worker_processes 2;
# nginx推荐设置worker的个数为cpu的核数
## 因为更多的worker数,会导致进程来竞争cpu资源了,从而带来不必要的上下文切换
error_log /log/nginx/error.log error;
# 日志输出级别有debug,info,notice,warn,error,crit可供,其中,debug输出日志最为详细,而crit输出日志最少。
pid /logs/nginx.pid;
# 指定进程id的存储文件位置,可以使用kill命令来发送相关信号。
## 例如如果想要重新读取配置文件,为`kill -HUP cat /var/run/nginx.pid`
### 如今可以通过nginx -s reload/stop 等指令优雅控制nginx
worker_rlimit_nofile 65535;
#Specifies the value for maximum file descriptors that can be opened by this process.
events
{
use epoll;
# 可选有select | poll | kqueue | rtsig | epoll | eventport | /dev/poll
## linux首选epoll
worker_connections 65535;
# 用于定义nginx每个进程的最大连接数,默认1024.
## 最大客户端连接由worker_processes和worker_connections决定,即`max_clients = worker_processes*worker_connections`
}
http {
server{
listen 80 default;
server_name _;
return 500;
}
# Nginx的server虚拟主机配置
## 禁止通过ip地址访问服务器文件目录
### 假如你的Nginx根目录设在”/home/user/www”,此时阻止别人通过”http://IP地址/blog”或”http://IP地址/forum”来访问你的站点。
### 如果你不想禁止IP地址访问整个目录,只是要防止别人通过IP访问你的博客。
### 在“server {}”中添加如下设置
### location ^~ /blog/ {deny all; }
include mime.types;
default_type application/octet-stream;
# 默认设置为二进制流,也就是当文件类型未定义时使用这种方式
#charset gb2312;
#设置访问的语言编码
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
# 指定来自客户端请求头的headebuffer大小
large_client_header_buffers 4 32k;
# 指定客户端请求中较大的消息头的缓存最大数量和大小,目前设置为4个32KB
client_max_body_size 8m;
# 设置允许客户端请求的最大的单个文件字节数
sendfile on;
# 开启高效文件传输模式
## 当 Nginx 是一个静态文件服务器的时候,开启 SENDFILE 配置项能大大提高 Nginx 的性能。 但是当 Nginx 是作为一个反向代理来使用的时候,SENDFILE 则没什么用。
tcp_nopush on;
# 在一个数据包里发送所有头文件,而不一个接一个的发送,防止网络阻塞
keepalive_timeout 60;
# 设置客户端连接保存活动的超时时间
tcp_nodelay on;
# 不要缓存数据,而是一段一段的发送
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;
# HttpGZip模块配置
gzip on;
# 开启gzip压缩
gzip_min_length 1k;
# 设置允许压缩的页面最小字节数
gzip_buffers 4 16k;
# 申请4个单位为16K的内存作为压缩结果流缓存
gzip_http_version 1.0;
# 设置识别http协议的版本,默认为1.1
gzip_comp_level 2;
# 指定gzip压缩比,1-9数字越小,压缩比越小,速度越快
gzip_types text/plain application/x-javascript text/css application/xml;
# 指定压缩的类型
gzip_vary on;
#是否发送Vary: Accept-Encoding响应头字段,通知接收方响应使用了gzip压缩
#limit_zone crawler $binary_remote_addr 10m;
log_format '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
##
- `$remote_addr` 与`$http_x_forwarded_for` 用以记录客户端的ip地址;
- `$remote_user`用来记录客户端用户名称;
- `$time_local` 用来记录访问时间与时区;
- `$request` 用来记录请求的url与http协议;
- `$status`用来记录请求状态,成功是200 ;
- `$body_bytes_s ent`记录发送给客户端文件主体内容大小;
- `$http_referer`用来记录从那个页面链接访问过来的;
- `$http_user_agent`记录客户端浏览器的相关信息
include /etc/nginx/conf/vhosts/*.conf;
# 新增vhosts目录,存放其他站点的配置文件
}
站点配置文件/etc/nginx/conf/vhosts/kachuan.blog.conf如下:
server {
listen 80;
# 监听端口80
server_name kachuan.blog.com;
# 设置主机域名
index index.html index.htm index.php;
# 设置虚拟主机默认访问的网页
root /home/kachuan.blog.com;
# 设置虚拟主机的网站根目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
# 设置图片文件缓存时间
location ~ .*\.(js|css)?$
{
expires 1h;
}
# 设置js/css样式文件缓存时间
access_log /nginx/access/phpwind.log;
# 设置虚拟主机访问日志的存放路径
}
# 注:多个站点可以在同一个文件中设置多个server
#user nobody;
worker_processes auto;
# 自动检测CPU核数并打开相同数量的worker进程
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
accept_mutex on;
multi_accept on;
}
http {
client_max_body_size 100M;
# 默认限制1M,当上传文件超过限制时会提示413错误
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 自定义日志格式main
access_log /var/log/nginx/access.log main;
# 按照main的日志格式记录访问日志
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 100;
types_hash_max_size 2048;
# 影响散列表的冲突率。
## types_hash_max_size越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。types_hash_max_size越小,消耗的内存就越小,但散列key的冲突率可能上升。
include /etc/nginx/mime.types;
# 加载 mime 文件后缀映射文件
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
# 加载模块配置文件
# 后台运行着blog和study两种服务,其中一种主机同时存在两种服务,位于不同端口,通过匹配url将不同的请求转发对应的端口上;blog服务则分布于多台主机,并通过weight分配不同的权重,达到负载均衡的目的
upstream blog{
server 192.168.190.128:5400 weight=5;
server 192.168.190.127:5400 weight=5;
}
# upstream的分配策略有:
## weight(权重):指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
## ip_hash(访问ip):每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
### upstream favresin{
### ip_hash;
### server 192.168.190.128:5400
### server 192.168.190.128:5400
### }
## fair(第三方): 按后端服务器的响应时间来分配请求,响应时间短的优先分配。
### upstream favresin{
### fair;
### server 192.168.190.128:5400
### server 192.168.190.128:5400
### }
## url_hash(第三方):按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
### upstream favresin{
### hash $request_uri;
### hash_method crc32;
### server 192.168.190.128:5400
### server 192.168.190.128:5400
### }
## 注意:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。
# upstream还可以为每个设备设置状态值,这些状态值的含义分别如下:
## down 表示单前的server暂时不参与负载
## weight 默认为1.weight越大,负载的权重就越大。
## max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误.
## fail_timeout : max_fails次失败后,暂停的时间。
## backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻
upstream study{
server 192.168.190.128:5100;
}
server {
listen 9000 default_server;
listen [::]:9000 default_server;
server_name _;
# _ 匹配任意域名
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location ^~ /study/ {
proxy_pass http://study;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
proxy_set_header Access-Control-Allow-Origin "*";
proxy_set_header Access-Control-Allow-Headers "Refer, Origin, User-Agent, X-Requested-With, Content-Type, Accept";
proxy_set_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, OPTIONS";
}
location / {
proxy_pass http://blog;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
proxy_set_header Access-Control-Allow-Origin "*";
proxy_set_header Access-Control-Allow-Headers "Refer, Origin, User-Agent, X-Requested-With, Content-Type, Accept";
proxy_set_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, OPTIONS";
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
nginx配置一(全局配置):https://lobert.iteye.com/blog/1929280
配置nginx:https://www.jianshu.com/p/81445f571590
Nginx 战斗准备 —— 优化指南:https://www.oschina.net/translate/nginx-setup?print
nginx优化 突破十万并发:https://www.cnblogs.com/sxlfybb/archive/2011/09/15/2178160.html
nginx官方文档:http://nginx.org/en/
Tengine官方文档:http://tengine.taobao.org/
linux添加开机启动项:https://blog.csdn.net/xiligey1/article/details/81205870
在Nginx上配置多个站点:https://www.cnblogs.com/Erick-L/p/7066564.html
sendonfile原理:http://xiaorui.cc/2015/06/24/扯淡nginx的sendfile零拷贝的概念/
Nginx 默认配置解析:http://blog.leanote.com/post/[email protected]/Nginx-默认配置解析
Nginx配置upstream实现负载均衡:https://blog.51cto.com/favccxx/1622091