nginx.conf 配置及参数说明

nginx.conf 配置及参数说明
 
#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       $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;

请特别注意加粗的一行,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等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nginx配置文件(nginx.conf)是Nginx服务器的主要配置文件,它定义了服务器的基本设置、监听选项、虚拟主机(server blocks)、路由规则和安全策略等。以下是nginx.conf的一些关键部分和配置详解: 1. **Server Block (虚拟主机)**: 每个`server`块代表一个独立的网站或服务,包含了域名、IP地址、端口、文档根目录等信息。例如: ``` server { listen 80; server_name example.com; root /var/www/example.com; } ``` 这里,Nginx监听80端口,当请求example.com时,它将从`/var/www/example.com`目录下提供内容。 2. **Listen and Connections**: `listen`指令指定Nginx监听的网络接口和端口。`backlog`参数设置连接队列长度。 ```yaml listen 80; # 默认监听HTTP listen [::]:443 ssl; # 双向绑定并加密HTTPS ``` 3. **Location Blocks (路由)**: `location`指令定义了URL路径的处理规则,包括文件服务、动态内容、反向代理等。例如: ``` location /api/ { proxy_pass http://backend/; } ``` 4. **Access Control**: `access_log`用于记录访问日志,`limit_rate`设置带宽限制,`auth_basic`用于基本身份验证。 ```bash access_log /var/log/nginx/access.log combined; limit_rate 100m; auth_basic "Restricted Area"; ``` 5. **Error Handling**: `error_page`定义错误代码对应的页面,`error_document`处理自定义错误。 ```nginx error_page 404 /404.html; error_page 500 /500.html; ``` 6. **SSL/TLS Configuration**: 如果需要使用HTTPS,会配置`ssl_certificate`, `ssl_certificate_key`以及加密算法等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值