您的位置:首页 > 运维架构 > Nginx

nginx学习手札(一)简单配置

2014-06-03 15:50 381 查看
首先需要安装PCRE包,为的是让nginx支持正则表达式:
下载地址:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
安装很简单,./configure&& make&&make install即可

nginx的安装就不赘述了,网上到处都有,我的编译参数是:
./configure --with-http_stub_status_module–prefix=/opt/nginx
其中参数 --with-http_stub_status_module 是为了启用 nginx 的NginxStatus 功能,用来监控 Nginx 的当前状态。
也需安装openssl

此配置文件仅仅提供最简单的web服务

#使用哪个用户启动nginx 前面是用户,后面是组

user nobody;

#nginx工作的进程数量,根据硬件调整,一般个CPU个数相等

worker_processes 20;

# [ debug | info | notice | warn | error | crit] 错误日志的位置

error_log logs/error.log crit;

#进程号保存文件

pid logs/nginx.pid;

#最大文件描述符

worker_rlimit_nofile 65535;

events {

useepoll; #使用epoll(linux2.6的高性能方式)

worker_connections 65535; #每个进程最大连接数(最大连接=连接数x进程数)

}

http {

include mime.types; #文件扩展名与文件类型映射表

default_type application/octet-stream; #默认文件类型

log_format main '$remote_addr -$remote_user [$time_local] '

'"$request_method $scheme://$host$request_uri $server_protocol"$status $body_bytes_sent '

'"$http_referer" "$http_user_agent"';

server_names_hash_bucket_size128; #指定服务器名称哈希表的框大小

client_header_buffer_size32k; #设定客户端请求的Header头缓冲区大小

large_client_header_buffers 4 32k;

client_max_body_size8m; #允许客户端请求内容的最大值

client_body_buffer_size32k; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存 到本地再传给用户

client_body_timeout 600; #客户端请求内容超时时间(状态码:408)
client_header_timeout 600; #客户端请求header头信息的超时时间(状态码:408)

# proxy_connect_timeout600; #nginx跟后端服务器连接超时时间

#proxy_read_timeout 600; #连接成功后,后端服务器响应时间

#proxy_send_timeout 600; #后端服务器数据回传时间(规定时间内必须传完所有数

#proxy_buffer_size 8k;

#proxy_buffers 4 32k;

# proxy_busy_buffers_size 64k;

# proxy_temp_file_write_size 64k; #缓存临时文件大小

sendfileon; #开启高效文件传输模式

#以下两个选项用于防止网络阻塞

tcp_nopush on;

tcp_nodelayon;

keepalive_timeout 65; #长链接超时时间

gzipon; #打开gzip压缩

gzip_min_length 1k; #最小压缩文件大小

gzip_buffers 4 16k; #压缩缓冲区

gzip_http_version 1.0; #压缩版本(默认1.1,前端为squid2.5使用1.0)

gzip_comp_level2; #压缩比 9为最大

gzip_types text/plain application/x-javascript text/cssapplication/xml; #压缩文件类型

gzip_varyon;

open_file_cache max=65535 inactive=20s;

open_file_cache_valid 30s;

open_file_cache_min_uses 1;

server_tokens off; #隐藏版本号

server{ #这是为了不允许通过IP来访问nginx,只能通过域名

listen 80 default;

server_name _;

return 500;

access_log off;

}

server{

listen 80 default;

server_name www.domain.com;

charset utf-8,GB2312;

index track.gif;

if (-d $request_filename) {

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

location / {

# autoindex on; 自动列目录,默认是off

root html;

access_log logs/l.access.log main;

#后端web服务器可以通过X-Forwarded-For获取用户IP

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $remote_addr;

}

location ~ ^/NginxStatus/ {

stub_status on;

access_log off;

if (-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

}

location ~ ^/(WEB-INF)/ {

deny all;

}

location ~ /purge(/.*) {

allow 127.0.0.1;

allow 192.168.0.0/16;

deny all;

}

error_page 404 http://www.domain.com/track.gif;
location /page-error {

rewrite ^(.*) http://www.domian/track.gif permanent;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

优化内核参数:
net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range =5000 65000

关于nginx的负载均衡,缓存策略以及反向代理等,慢慢在学习~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: