nginx安装与配置

来源:互联网 发布:怎么找淘宝模特的工作 编辑:程序博客网 时间:2024/04/26 17:19
系统环境:CentOS 5.6 64位,下载nginx安装包。
 
1、安装前,需要安装的软件包:
yum -y install gcc gcc-c++ autoconf automake
 
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
 
2、安装nginx
tar zxvf nginx-1.2.1.tar.gz
cd nginx-1.2.1/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
 
3、启动与停止nginx
启动:usr/local/sbin/nginx
停止:pkill -9 nginx
 
4、根据需要修改文件:nginx.conf(默认路径为:/usr/local/nginx/conf/nginx.conf)
 
#使用的用户和组
user www www;
#指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍,例如两个四核CPU,则总核数为8)
worker_processes 8;
#指定错误日志存放的路径,错误日志记录级别可选项为:[debug|info|notice|warn|error|crit]
error_log logs/error.log crit;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定PID存放的路径
pid logs/nginx.pid;
#指定文件描述符数量
 
worker_rlimit_nofile 50000;
 
events {
 #使用的网络I/O模型,Linux系统推荐采用epoll模型,FreeBSD 系统推荐采用kqueue模型
    use epoll;
 #允许的连接数
    worker_connections 50000;
}
 
 
http {
    include mime.types;
    default_type application/octet-stream;
    #设置使用的字符集,如果一个网站有多种字符集,请不要随便设置,应让程序员在html代码中通过meta标签设置
 
    #charset gb2312;
 
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
 
    #设置客户端能够上传的文件大小
    client_max_body_size 8m;
 
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 65;
 
    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;
 
    #开启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;
 
    log_format access '$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 access;
 
    server {
        listen 80;
        server_name 127.0.0.1;
 
        #index index.html index.htm index.php
        #root /data0/htdocs;
        #charset koi8-r;
 
        #access_log logs/host.access.log main;
 
        location / {
            root html;
            #root /var/www/web;#应用存在路径
            index index.html index.htm index.php;
        }
 
        #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;
        #}              
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
           expires 30d;
        }
 
        location ~ .*\.(js|css)?$
        {
           expires 1h;
        } 
    }
 
 
    # 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;
    # server_name localhost;
    # ssl on;
    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.key;
    # ssl_session_timeout 5m;
    # ssl_protocols SSLv2 SSLv3 TLSv1;
    # ssl_ciphers HIGH:!aNULL:!MD5;
    # ssl_prefer_server_ciphers on;
    # location / {
    # root html;
    # index index.html index.htm;
    # }
    #}
}
 
5、检验文件:nginx.conf是否出错的:/usr/local/nginx/sbin/nginx -t
修改/conf/nginx.conf 文件后,查检是否正确,如正确,显示:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
 
6、修改文件nginx.conf后,可以平滑重启nginx服务:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
 
7、需要定时转移日志文件:/usr/local/nginx/logs/access.log
这个日志文件时间久了会比较大,转移的脚本如下:
#!/bin/sh
logs_path="/usr/local/nginx/logs/"
mkdir -p ${logs_path}$(date +%Y)/$(date +%m)/
mv ${logs_path}access.log  ${logs_path}$(date +%Y)/$(date +%m)/access_$(date +%Y%m%d).log
Kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
 
注:
Kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`  重新打开日志文件,在切割日志时用途较大。
原创粉丝点击