Nginx
是一个高性能的HTTP和方向代理服务器,在现今互联网环境广泛使用,通常情况下,我们可以通过nginx
实现url
重写、浏览器缓存配置、gzip压缩、反向代理、负载均衡、https的配置等。本文将对Nginx
单机环境安装及配置文件进行简单的介绍。
nginx 1.13.6
tar -zxvf nginx-1.13.6.tar.gz
cd nginx-1.13.6
./configure –prefix=/root/opt/nginx
make && make install
启动nginx
sbin/nginx
检测nginx配置文件
sbin/nginx -t
加载配置文件
sbin/nginx -s reload
停止nginx
sbin/nginx -s stop
或sbin/nginx -s quit
# 使用的用户
#user nobody;
# 指定工作进程数(一般等于CPU的总核数或总核数的两倍,如两个4核CPU,则总核数为8)
worker_processes 1;
# 指定错误日志存放的路径,如notice、info等为错误日志的基本,为可选项,包括[debug | info | notice | warn | error]等
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定pid存放的路径
#pid logs/nginx.pid;
events {
# 使用的网络IO模型,Linux系统推荐采用epoll模型
use epoll;
# 允许的最大连接数
worker_connections 1024;
}
http {
include mime.types;
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;
sendfile on;
#tcp_nopush on;
# 设置客户端能够上传的文件大小
client_max_body_size 5m;
#keepalive_timeout 0;
keepalive_timeout 65;
# 开启gzip压缩
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 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 https配置部分
#
#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;
# }
#}
}
虚拟主机使用的是特殊的软硬件技术,它把一台允许在因特网上的服务器主机分成多台“虚拟”的主机,每台虚拟主机可以是一个独立的网站,可以具有单独的域名或者具有完整的Internate服务器功能,在同一台主机上的虚拟主机之间是完全独立的。在Nginx中利用虚拟主机,不用为每个运行的网站提供一台单独的Nginx服务器。虚拟主机提供了一台服务器,同一nginx进程上运行多个网站的功能。虚拟主机配置一般包括三种,即基于端口的虚拟主机、基于IP的虚拟主机、基于域名的虚拟主机。本文只介绍基于域名的虚拟主机的配置,如下:
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# 虚拟主机
server {
# 监听端口
listen 80;
# 主机名称
server_name www.garlic.com;
#access_log logs/host.access.log main;
location / {
# 网站文件存放目录
root 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;
}
}
}
从以上配置可以看出,一个server段对应一个虚拟主机的配置,如果需要配置多个虚拟主机,增加多个server段即可。
在生产环境中,由于访问日志文件增长速度较快,日志太大会严重影响服务器效率,同时,为了方便对日志进行分析,须要对日志文件进行定时切割,定时切割的方式有多种,最常见的是按照天来进行切割。讲到日志切割,首先必须了解日志配置,日志配置的方式如下:
http {
include mime.types;
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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.garlic.com;
#charset koi8-r;
# 针对单个虚拟主机的日志配置
access_log logs/garlic.access.log main;
location / {
root 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;
}
}
}
日志切割的原理是通过基于Linux的定时任务,然后进行文件的mv
操作,通过向nginx主进程发送 kill -USR1 nginx [主进程号]
的方式进行日志切割
#!/bin/sh
# log_cut.sh
# 用于实现nginx日志切割
#
# Created by iOS-Dev on 21/12/2017.
#
## nginx 日志文件所在目录
LOGS_PATH=/root/opt/nginx/logs
## 获取昨天的日期yyyy-MM-dd
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
## 移动文件
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log
## 向nginx主进程发送USR1信号,USR1 信号是重新打开日志文件
kill -USR1 $(cat /root/opt/nginx/logs/nginx.pid)
./configure --prefix=/root/opt/nginx
时提示 C compiler cc is not found
详细错误信息:
[root@localhost nginx-1.13.6]# ./configure --prefix=/root/opt/nginx
checking for OS
+ Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
解决方法:
yum install gcc
./configure --prefix=/root/opt/nginx
时提示./configure: error: the HTTP rewrite module requires the PCRE library.
详细错误信息:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
解决方法:
yum install pcre-devel
./configure --prefix=/root/opt/nginx
时提示./configure: error: the HTTP gzip module requires the zlib library.
详细错误信息:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib= option.
解决方法:
yum install zlib-devel