nginx配置文件nginx.conf

这里使用 nginx负载两个tomcat服务javascript

nginx.conf文件 

#user  nobody;#windows 不配置
#工做的子进程数量(一般等于CPU数量或者2倍于CPU)
worker_processes  8;
#错误日志存放路径,默认地址在nginx路径下的logs下
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;
#指定pid存放文件
#pid        logs/nginx.pid;


events {
    #容许最大链接数
    worker_connections  2048;
}


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;
    gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types    text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
    upstream mytomcat{#mytomcat与 location的 proxy_pass的值对应
    ip_hash;#确保每一个访问的ip每次sessoion只访问到一个服务
    #weigth参数表示权值,权值越高被分配到的概率越大
    #server 127.0.0.1:8080  weight=1 ;
    server 127.0.0.1:8080;#tomcat6
    server 127.0.0.1:8888;#tomcat6
    }

    server {
        listen       80;
        server_name  mpc;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.jsp index.html index.htm;
            proxy_pass http://mytomcat;#upstream 对应
            proxy_set_header Host $host; #后端的Web服务器能够经过X-Forwarded-For获取用户真实IP。
            client_max_body_size 10m; #容许客户端请求的最大单文件字节数。
            client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数。
            proxy_connect_timeout 3; #Nginx跟后端服务器链接超时时间。超时会链接到另一台服务器
            proxy_read_timeout 90; #链接成功后,后端服务器响应时间。
            proxy_buffer_size 4k; #设置代理服务器保存用户头信息的缓冲区大小。
            proxy_buffers 6 32k; #proxy_buffers缓冲区。
            proxy_busy_buffers_size 64k; #高负荷下缓冲大小。
            proxy_temp_file_write_size 64k; #设定缓存文件夹大小。
        }

        #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;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

直接下载pdf等文件配置

location ~ .*\.(txt|pdf|doc|xls|docx|xlsx|zip|rar)$  {
              if ($request_filename ~* ^.*?\.(txt|pdf|doc|xls|docx|xlsx|zip|rar)$){
                    add_header Content-Disposition: 'attachment;';
          }
            root   /app/html;
            index  index.html index.htm;
        }php

几个经常使用命令  

start nginx #启动nginx
nginx -s quit #关闭nginx  慢慢关闭?
nginx -s stop #中止nginx 跟关闭相似,直接关闭?
nginx -s reload #修改了配置文件从新加载

定时切割nginx访问日志文件

        随着访问量愈来愈大,nginx的日志文件会不断增大,会致使日志写入时间增长,因此定时备份切割日志文件是必须的,这里写一个windows下定时将日志文件备份状况的命令行:css

@echo off
echo 备份日志文件
#取到当前全部日志文件数量
for /f %%i in ('dir *.log /b/a-d') do set /a num+=1
#备份日志文件
copy access.log access%num%.log
#将一个空日志文件覆盖原来的日志文件
copy /y accessnull.log access.log
echo ... 
echo ... 
echo 备份完成! 
#pause
相关文章
相关标签/搜索