nginx配置说明

nginx配置说明
#运行用户
user  nobody nobody;
#启动进程
worker_processes  2;
#全局错误日志及PID文件
error_log  logs/error.log notice;
pid        logs/nginx.pid;
#工作模式及连接数上限
events {
use epoll;
worker_connections      1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型
include      conf/mime.types;
default_type  application/octet-stream;
#设定日志格式
log_format main        '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#设定请求缓冲
client_header_buffer_size    1k;
large_client_header_buffers  4 4k;
#开启gzip模块
gzip on;
gzip_min_length  1100;
gzip_buffers    4 8k;
gzip_types      text/plain;
output_buffers  1 32k;
postpone_output  1460;
#设定access log
access_log  logs/access.log  main;
client_header_timeout  3m;
client_body_timeout    3m;
send_timeout          3m;
sendfile                on;
tcp_nopush              on;
tcp_nodelay            on;
keepalive_timeout  65;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的机率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80  weight=1;
server 192.168.8.3:80  weight=6;
}
#设定虚拟主机
server {
listen          80;
server_name    192.168.8.1 www.yejr.com;
charset gb2312;
#设定本虚拟主机的访问日志
access_log  logs/www.yejr.com.access.log  main;
#如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid
#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(img|js|css)/  {
root    /data3/Html;
expires 24h;
}
#对 "/" 启用负载均衡
location / {
proxy_pass      http://mysvr;
proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout  90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size      4k;
proxy_buffers          4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status            on;
access_log              on;
auth_basic              "NginxStatus";
auth_basic_user_file  conf/htpasswd;
}
}
}
备注:conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可,内容大致如下:
  3.) 查看 Nginx 运行状态
输入地址 http://192.168.8.1/NginxStatus/,输入验证帐号密码,即可看到类似如下内容:
Active connections: 328
server accepts handled requests
9309    8982        28890
Reading: 1 Writing: 3 Waiting: 324
第一行表示目前活跃的连接数
第三行的第三个数字表示Nginx运行到当前时间接受到的总请求数,如果快达到了上限,就需要加大上限值了。
第四行是Nginx的队列状态
-----------------------------------------------------------------------------------------------------------------------------
Nginx配置防盗链
有个客户的站点负载过高,于是我们可爱的愤青系统工程师将其的Apache换成Nginx,效果很明显。现在记录一些Apache转换过程中的小细节,留下备忘。
Nginx的防盗链
一般的防盗链如下:
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.xxx.com www.xxx.net;
if ($invalid_referer) {
rewrite ^/ http://www.xxxcom/403.html;
#return 404;
}
}
第一行:gif|jpg|png|swf|flv
表示对gif、jpg、png、swf、flv后缀的文件实行防盗链
第二行:www.xxx.com www.xxx.net
表示对www.xxx.com www.xxx.net这2个来路进行判断
if{}里面内容的意思是,如果来路不是指定来路就跳转到错误页面,当然直接返回404也是可以的。
NginxHttpAccessKeyModule实现防盗链
如果不怕麻烦,有条件实现的话,推荐使用NginxHttpAccessKeyModule这个东西。
他的运行方式是:如我的download 目录下有一个 file.zip 的文件。对应的URI 是http://www.xxx.com/download/file.zip
使用ngx_http_accesskey_module 模块后http://www.xxx.com/download/file.zip?key=09093abeac094. 只有给定的key值正确了,才能够下载download目录下的file.zip。而且 key 值是根据用户的IP有关的,这样就可以避免被盗链了。
据说NginxHttpAccessKeyModule现在连迅雷都可以防了,可以尝试一下。
-----------------------------------------------------------------------------------------------------------------------------
Nginx下配置负载均衡
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。
首先是配置十分的简单,而且功能非常强大。真是相见恨晚。
先来看看配置文件怎么写吧
worker_processes 1;
events {
worker_connections 1024;
}
http{
upstream myproject {
#这里指定多个源服务器,ip:端口,80端口的话可写可不写
server 192.168.43.158:80;
server 192.168.41.167;
}
server {
listen 8080;
location / {
proxy_pass http://myproject;
}
}
}
nginx的负载均衡有哪些功能呢?
如果后面的服务器其中一台坏了,它能自动识别,更牛的是它好了之后nginx可以马上识别
服务器A和B,如果A的响应时间为3,B的响应时间为1,那么nginx会自动调整访问B的概率是A的3倍,真正做到负载均衡
----------------------------------------------------------------------------------------------------------------------------
在Nginx下支持泛域名解析
在Nginx下支持泛域名解析其实很简单.只要在在编译 Nginx的时候加上以下代码即可
--with-http_sub_module
在配置nginx时:
server {
        # Replace this port with the right one for your requirements
        listen      80;
 #could also be 1.2.3.4:80
 
        # Multiple hostnames seperated by spaces.  Replace these as well.
        server_name  www.yourdomain.com *.yourdomain.com;
        #Alternately: _ *
        root /PATH/TO/WEBROOT/$host;
        error_page  404              http://yourdomain.com/errors/404.html;
        access_log  logs/yourdomain.com.access.log;
        location / {
            root  /PATH/TO/WEBROOT/$host/;
            index  index.php;
        }
 
        # serve static files directly
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
            access_log        off;
            expires          30d;
        }
 
        location ~ .php$ {  
          # By all means use a different server for the fcgi processes if you need to  
          fastcgi_pass  127.0.0.1:YOURFCGIPORTHERE;
          fastcgi_index  index.php;
 
          fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
          fastcgi_param  QUERY_STRING    $query_string;
          fastcgi_param  REQUEST_METHOD  $request_method;
          fastcgi_param  CONTENT_TYPE    $content_type;
          fastcgi_param  CONTENT_LENGTH  $content_length;
          fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
          fastcgi_param  REQUEST_URI        $request_uri;
          fastcgi_param  DOCUMENT_URI      $document_uri;
          fastcgi_param  DOCUMENT_ROOT      $document_root;
          fastcgi_param  SERVER_PROTOCOL    $server_protocol;
          fastcgi_param  REMOTE_ADDR        $remote_addr;
          fastcgi_param  REMOTE_PORT        $remote_port;
          fastcgi_param  SERVER_ADDR        $server_addr;
          fastcgi_param  SERVER_PORT        $server_port;
          fastcgi_param  SERVER_NAME        $server_name;
          fastcgi_intercept_errors on;
        }
 
        location ~ /\.ht {
            deny  all;
        }
    }
然后启动nginx就可以实现泛域名解析了
-----------------------------------------------------------------------------------------------------------------------------
nginx的虚拟主机
安装好了Nginx,下面就要考虑详细配置了。我需要把我原来运行在apache上的若干个虚拟主机以及ssl站点都迁移到Nginx上来。
在这之前,你可能需要调整你的nginx.conf这个配置文件,修改一下里面的一些必备参数,比如nginx的运行用户等等。
我没有使用默认的配置,而是自己重新写了一份,仅供参考:

user httpd httpd;
 
worker_processes 10;
 
pid /usr/local/nginx/nginx.pid;
 
worker_rlimit_nofile 51200;
 
events
{
 use epoll;
 worker_connections 51200;
}
 
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'
 tcp_nopush on;
 tcp_nodelay off;
 
 keepalive_timeout 60;
 client_header_timeout 3m;
 client_body_timeout 3m;
 send_timeout 3m;
 connection_pool_size 256;
 client_header_buffer_size 1k;
 large_client_header_buffers 4 2k;
 request_pool_size 4k;
 output_buffers 4 32k;
 postpone_output 1460;
 client_max_body_size 10m;
 client_body_buffer_size 256k;
 client_body_temp_path /dev/shm/client_body_temp;
 proxy_temp_path /usr/local/nginx/proxy_temp;
 fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
 
 gzip on;
 gzip_http_version 1.0;
 gzip_comp_level 2;
 gzip_proxied any;
 gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
 gzip_min_length 1100;
 gzip_buffers 4 8k;
 
 #和apache类似,Nginx也可以使用include指令包含一系列的配置文件,我将虚拟主机的配置统一放在了
 #/usr/local/nginx/conf/vhosts目录下
 include vhosts/*.conf;
 
 error_log /usr/local/nginx/logs/error.log;
 access_log /usr/local/logs/access.log combined;
}
在/usr/local/nginx/conf目录下新建文件php_fcgi.conf,保存php的fastcgi设置。我的配置文件如下:
fastcgi_pass  127.0.0.1:19000;
fastcgi_index index.php;
 
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
#new ac upload
#fastcgi_pass_request_body off;
#client_body_in_file_only clean;
#fastcgi_param  REQUEST_BODY_FILE  $request_body_file;
#
 
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
 
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
 
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
这里注意第一行fastcgi_pass,里面的ip地址以及端口要与在前面配置的spawn-fcgi里面指定的ip与端口一致,否则Nginx无法将php的请求正确的传递到php的fastcgi守护进程。
配置好了php的fastcgi以后,下面可以进行每一个虚拟主机的配置了,以我的ipbfans.org为例:
server
{
 listen 80;
 server_name www.ipbfans.org ipbfans.org doc.ipbfans.org from1979.cn www.from1979.cn;
 
 index index.php index.html index.htm;
 root /usr/local/nginx/www;
 
 location ~ .*\.php?$
 {
  include php_fcgi.conf;
 }
 
 access_log /usr/local/nginx/logs/ipbfans.org/access.log combined;
 error_log /usr/local/nginx/logs/ipbfans.org/error.log; 
}
如果有多个php的虚拟主机,参照这个配置文件,修改root选项,以及log的保存目录就可以了。
-----------------------------------------------------------------------------------------------------------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章