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

nginx.conf 配置及参数说明

2018-02-24 08:36 375 查看
[align=center] nginx.conf 配置及参数说明[/align]  
#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;

events {
    worker_connections  10240;#连接数,在应用中发现,连接数小会有网络请求很慢的情况
}

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;
upstream front {  #代理配置
   ip_hash;
   server 127.0.0.1:8080;
   server 10.7.12.32:9080 weight=2;

 
   
   }
    server {
        listen       80; #服务器端口
    client_header_buffer_size 16k;
        large_client_header_buffers 4 64k;
    client_max_body_size 15m;
        server_name  http://10.7.11.76; #服务器地址
        location /  {
            proxy_pass http://front; #代理标识
             proxy_redirect off ;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header REMOTE-HOST $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        
   
    location /upload {
            alias  d:/upload; #映射磁盘访问路径
        }
   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    
    }

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

}

检测nginx配置文件是否正确
/usr/local/nginx/sbin/nginx -t -c nginx.conf-c 配置文件路径
-g Set global directives. (version >=0.7.4)
-t 检测文件是否正确不执行
-v Print version.
-V Print nginx version, compiler version and configure parameters.
编译时如果使用了–with-debug编译,还可以使用error_log file [ debug_core| debug_http | debug_event …] 来获得debug信息
通过信号对Nginx进行控制Nginx支持下表中的信号:
信号名     作用描述
TERM, INT  快速关闭程序,中止当前正在处理的请求
QUIT           处理完当前请求后,关闭程序
HUP            重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求
USR1          重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件
USR2          平滑升级可执行程序
WINCH        从容关闭工作进程
有两种方式来通过这些信号去控制 Nginx,第一是通过 logs 目录下的 nginx.pid 查看当前运行的 Nginx 的进程 ID,通过 kill – XXX <pid> 来控制 Nginx,其中 XXX 就是上表中列出的信号名。如果您的系统中只有一个 Nginx 进程,那您也可以通过 killall 命令来完成,例如运行 killall – s HUP nginx 来让 Nginx 重新加载配置。
配置:
use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];FreeBSD使用kqueue,Linux选epoll.
worker_connections number    每个worker的最大连接数
Maxclient = work_processes * worker_connections
nginx的upstream目前支持4种方式的分配
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
2、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
3、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
4、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
代理
只需要在nginx的配置文件中增加虚拟主机,然后加入
\proxy_pass http://localhost:8000;负载均衡:
只需要在http中增加
upstream tgcluster {#定义负载均衡设备的Ip及设备状态
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://tgcluster/;
每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
FASTCGI配置:
请将以下内容保存为fastcgi_params文件,保存于/usr/local/nginx/conf下(Ubuntu可保存于/etc/nginx下),他为我们的FastCGI模块设置了基本的环境变量:
#fastcgi_params
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $cont
4000
ent_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;
请特别注意加粗的一行,PHP-CGI特别需要此行信息来确定PHP文件的位置。
另外需要在PHP-CGI的配置文件(Ubuntu 上此配置文件位于/etc/php5/cgi/php.ini)中,打开cgi.fix_pathinfo选项:
cgi.fix_pathinfo=1;
这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。
接下来在nginx的配置中针对php文件配置其利用FastCGI进程来执行:
server {
index index.php;
root  /usr/local/nginx/html;
location ~ .*.php$ {
include /usr/local/nginx/conf/fastcgi_params;  #请根据自己保存的路径进行设置
fastcgi_index index.php;
fastcgi_pass  127.0.0.1:9000; #请根据自己的FastCGI绑定的地址和端口进行配置
}
}
通知Nginx重新载入配置:
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
Ubuntu用户可以使用init脚本:sudo /etc/init.d/nginx reload
然后启动php-cgi -b 127.0.0.1:9000
如果出现No input file specified表示SCRIPT_FILENAME设置的有问题。
使用lighttpd的 spawn-fcgi
get http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #获取Lighttpd的源码包
tar -xvjf lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure  #编译
make
cp src/spawn-fcgi /usr/local/bin/spawn-fcgi #取出spawn-fcgi的程序
下面我们就可以使用 spawn-fcgi 来控制php-cgi的FastCGI进程了
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi
参数含义如下
-f <fcgiapp> 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置
-a <addr> 绑定到地址addr
-p <port> 绑定到端口port
-s <path> 绑定到unix socket的路径path
-C <childs> 指定产生的FastCGI的进程数,默认为5(仅用于PHP)
-P <path> 指定产生的进程的PID文件路径
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: