nginx组成
nginx由 二级制文件,config 配置文件 access.log error.log组成
nginx如何选择发行版本
主线版本(MainLine) : 一般会存在一些问题
稳定版(Stable): 运行稳定可以选择
nginx安装
官网:http://nginx.org/en/download.html 下载
通过yum等工具直接安装(不推荐):
直接安装nginx binary 有个大问题,nginx的二进制文件是会把模块直接编译进来的,nginx的官方模块并不是每一个默认都会开启,如果想添加第三方模块 必须通过编译nginx来实现
编译安装:
#安装必要lib
yum -y install gcc-c++
yum -y install pcre-devel
yum install -y zlib-devel
1.下载nginx
通过官网下载
2.nginx各个目录
CHANGES: 提供了哪些新特性 和bugfix
CHANGES.ru: 因为作者是个俄罗斯人 所以会有一个俄罗斯语言的配置文件
conf: 配置示例文件,nginx安装完成后方便运维人员配置
configure: 用于生成一些中间文件,用于编译前的一些必备动作
contrib: nginx语法提示
html: 提供了两个标准html 首页和错误页
man: linux对nginx的一个帮助文件
src: nginx的源代码
3.configure
支持一些参数 用于安装 例如 --prefix 指定nginx的安装目录
configure --help 可以查看帮助文档
./configure --prefix /usr/local/nginx-1.8
nginx配置的特性 以及安装目录
nginx path prefix: "/usr/local/nginx-1.8.18"
nginx binary file: "/usr/local/nginx-1.8.18/sbin/nginx"
nginx modules path: "/usr/local/nginx-1.8.18/modules"
nginx configuration prefix: "/usr/local/nginx-1.8.18/conf"
nginx configuration file: "/usr/local/nginx-1.8.18/conf/nginx.conf"
nginx pid file: "/usr/local/nginx-1.8.18/logs/nginx.pid"
nginx error log file: "/usr/local/nginx-1.8.18/logs/error.log"
nginx http access log file: "/usr/local/nginx-1.8.18/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
4.中间文件
objs:
Makefile
ngx_auto_config.h
ngx_auto_headers.h
ngx_modules.c : 那些模块会被编译到nginx中
5.编译
make
6.安装
make install
nginx常用指令
命令格式: nginx (-s 发送指令, -p 指定运行目录, -g 指定配置指令覆盖配置文件中的指令,-c 指定配置文件, -t 检查配置文件是否有错误 -v -V 打印nginx版本信息 )
stop: 立刻停止服务
quit: 优雅的停止服务
reload: 重载配置文件
reopen: 重新记录访问日志
nginx配置
niginx配置文件由指令 和 指令块组成 下面是nginx初始配置文件:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip 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
#
#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;
搭建一可用的静态web服务器
#指定路径与目录对应:
location / {
alias /usr/local/xie/; #指定资源目录
#autoindex on;
#set $limit_rate 1k;
#root html;
#index index.html index.htm;
}
gzip压缩
server{
gzip on; #开启gzip
gzip_comp_level 6; #压缩级别
gzip_min_length 100; #文件压缩最小大小
gzip_types application/javascript text/css text/xml; # 文件类型
gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
}
文件分享
location / {
alias /usr/local/tools/; #指定资源目录
autoindex on;
}
限制响应速度
location / {
alias /usr/local/tools/; #指定资源目录
autoindex on;
set $limit_rate 1k; #限制响应速度为 1kb/s
}
nginx日志配置
http{
#...
#声明日志格式 日志格式名称 格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#...
server{
#...
#指定日志文件位置 日志格式
access_log logs/main.access.log main;
#...
}
}