龚哥哥 - 山里男儿 爱生活、做自己!
Nginx配置多个虚拟机
发表于 2015-8-31 | 服务器
虚拟机分开管理方式
nginx.conf 文件配置如下

#运行用户
user www www;

#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

#全局错误日志
error_log  /data/log/error.log;

#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
    use epoll;

    #单个后台worker process进行的最大并发连接数
    worker_connections  65535;
}

http{
    #配置信息
    include mime.types; #文件扩展名与文件类型映射表
    default_type application/octet-stream; #默认文件类型

    #charset utf-8; #默认编码
    server_names_hash_bucket_size 128; #服务器名字的hash表大小
    client_header_buffer_size 32k; #上传文件大小限制
    large_client_header_buffers 4 64k; #设定请求缓
    client_max_body_size 8m; #设定请求缓
    sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
    tcp_nopush on; #防止网络阻塞
    tcp_nodelay on; #防止网络阻塞
    keepalive_timeout 120; #长连接超时时间,单位是秒

    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
    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压缩输出
    gzip_min_length 1k; #最小压缩文件大小
    gzip_buffers 4 16k; #压缩缓冲区
    gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 2; #压缩等级
    #gzip_types text/plain text/css text/javascrip application/javascript application/x-javascript image/jepg image/gif image/png;
    gzip_types text/plain text/css text/javascrip application/javascript application/x-javascript;
    #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;
    #limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

    #文件缓存
    open_file_cache max=65535 inactive=60s; #这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
    open_file_cache_valid 80s; #这个是指多长时间检查一次缓存的有效信息。
    open_file_cache_min_uses 1; #参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

    #开启404
    fastcgi_intercept_errors on;
    #载入虚拟机配置文件, 载入vhost目录下的所有配置文件, 目录位与nginx.conf同级
    include vhost/*.conf;
}

vhost/demo.conf 文件配置如下
# https
server {
        listen443;
        server_namewww.gongfuxiang.com gongfuxiang.com *.gongfuxiang.com;
        if ( $host != 'www.gongfuxiang.com' ) {
        rewrite ^/(.*)$ https://www.gongfuxiang.com/$1 permanent;
    }
    access_log/data/log/access.log;
    error_log/data/log/error.log warn;
    indexindex.html index.htm index.php;
    root/data/www;
    autoindexoff;
    include/data/www/.htaccess;

    sslon;
    ssl_certificate/data/https_rsa/gongfuxiang.pem;
    ssl_certificate_key/data/https_rsa/gongfuxiang.key;
    ssl_session_timeout5m;

    #4000,500错误定义
    error_page  404 http://$server_name;
    #error_page  500 502 503 504  /50x.html;

    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    #JS和CSS缓存时间设置
    location ~ .*.(js|css)?$
    {
        expires 1h;
    }

    #所有静态文件由nginx直接读取不经过tomcat或resin
    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
    {
        expires 15d;
    }
}

# http
server {
    listen80 default;
    server_namewww.gongfuxiang.com gongfuxiang.com *.gongfuxiang.com;
    if ( $host != 'www.qualifes.com' ) {
        rewrite ^/(.*)$ http://www.gongfuxiang.com/$1 permanent;
    }
    access_log/data/log/access.log;
    error_log/data/log/error.log warn;
    indexindex.html index.htm index.php;
    root/data/www;
    autoindexoff;
    include/data/www/.htaccess;
    #400,500错误定义
    error_page  404 http://$server_name;
    #error_page  500 502 503 504  /50x.html;

    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    #JS和CSS缓存时间设置
    location ~ .*.(js|css)?$
    {
        expires 1h;
    }

    #所有静态文件由nginx直接读取不经过tomcat或resin
    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
    {
        expires 15d;
    }
}

如果有新的虚拟机添加,则在vhost目录下拷贝一个虚拟机配置文件进行修改项目路径以及其它配置即可。

发表评论:

TOP