1、Nginx 在启动后,会有一个 master 进程和多个相互独立的 worker 进程。 

2、接收来自外界的信号,向各worker进程发送信号,每个进程都有可能来处理这个连接。 

3、master 进程能监控 worker 进程的运行状态,当 worker 进程退出后(异常情况下),会自动启动新的 worker 进程。

a.png



一、创建nginx启动脚本

       说明:nginx默认没有启动脚本(apache可以通过apachetl来操作);该脚本包含有start、stop、reload、restart、configtest功能

        vim /etc/init.d/nginx


        内容如下:

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL


二、 授予755权限,加入系统服务,开机启动

        命令 chmod 755 /etc/init.d/nginx

        命令 chkconfig --add nginx

        命令 chkconfig nginx on


        注:检查配置文件命令 service nginx configtest

                                           /usr/local/nginx/sbin/nginx -t


三、编辑配置nginx主配置文件

       说明:默认的配置文件,分两个部分:整体配置和server配置(即虚拟主机部分)

        vim /usr/local/nginx/conf/nginx.conf

       内容如下:

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
# 如果遇到较为少见的错误,我们可以修改nginx的错误日志级别,使其记录最多的日志内容,这样方便我们排查错误,将 crit 改为 debug

pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;

server
{
    listen 80;
    server_name localhost;
    index index.html index.htm index.php;
    root /usr/local/nginx/html;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
    # nginx.conf和php-fpm.conf监听方式必须统一,否则会报502,而且socket文件的路径一定要对

        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }

}

}


日志格式:

$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址
$remote_user :用来记录客户端用户名称
$time_local : 用来记录访问时间与时区
$request : 用来记录请求的url与http协议
$status : 用来记录请求状态;成功是200


四、一般采用主配置文件中开启子配置文件的方法

      说明:主配置文件中,主机部分无需填写,新建一个虚拟主机配置文件


    【主配置文件】

      vim /usr/local/nginx/conf/nginx.conf

      内容如下:

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
     include vhosts/*.conf;
}


【子配置文件】

     说明:/usr/local/nginx/conf目录下创建vhosts目录(所有虚拟主机配置文件在该目录下),并创建一个默认配置文件default.conf


 配置文件一:

    说明:default_server,无论主机解析什么域名,都会走这个虚拟主机和配置;root,为了限制上面的情况,把一台默认主机弄成403forbidden,比如root弄到/tmp/1233目录;location php,无需再配置


    命令 mkdir /tmp/1233


    vim /usr/local/nginx/conf/vhosts/default.conf

    内容如下:

server
{
    listen 80 default_server;
    server_name localhost;
    index index.html index.htm index.php;
    root /tmp/1233;
    deny all;
}


  配置文件二:

    说明:真正虚拟主机配置文件;目录可以设置为discuz论坛目录,php-fpm采用ip+端口的监听方式(socket方式会502报错)


  vim /usr/local/nginx/conf/vhosts/huangzhenping.conf

    内容如下:

server
{
    listen 80;
    server_name huangzhenping.cn;
    index index.html index.htm index.php;
    root /data/www;

    location ~ \.php$ {
        include fastcgi_params;
         #fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }

}

 

location规则说明:

         =    开头表示精确匹,如 A 中只匹配根目录结尾的请求,后面不能带任何字符串

         ^~ 开头表示uri以某个常规字符串开头,不是正则匹配

         ~    开头表示区分大小写的正则匹配

         ~*  开头表示不区分大小写的正则匹配

          /    通用匹配,如果没有其它匹配,任何请求都会匹配到       

         @   定义一个命名的 location,使用在内部定向时,例如 error_page,try_files


顺序优先级: (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)