Nginx 初探

Nginx 初探

engine x. 高性能http和反向代理服务器。也是一个IMAP,POP3,SMTP代理服务器。

 

编译安装:

解压: tar -zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2/
./configure --prefix=/usr/local/nginx

./configure: error: C compiler cc is not found

# yum install gcc gcc-c++ ncurses-devel perl

./configure: error: the HTTP rewrite module requires the PCRE library.

#yum install -y pcre pcre-devel

./configure: error: the HTTP gzip module requires the zlib library.
# yum install -y zlib-devel
make && make install

启动 ./sbin/nginx

 

启动过程中,如果报错:

nginx:[emerg] bind() to 0.0.0.0:80 failer (98: Address alreay in use)

表示80端口已被占用,把占用的80端口的服务或者软件关闭即可

查看端口及运行程序:

netstat -antp(表示程序)

杀掉进程:kill -9 pid ;如果还杀不掉 就pkill -9 pid

信号控制&进程管理



 

TERM | INT 快速关闭(不要轻易用)

kill -INT pid

 

QUIT 优雅的关闭,请求结束后在关闭

 kill -QUIT pid 等价于 ./sbin/nginx -s stop

 

HUP 开启新的子进程,然后配置文件改变,读新的配置文件,然后优雅关闭旧的子进程,所以Nginx 改完配置不必重启主进程就能使得新改的配置文件生效

kill -HUP pid  等价于 ./sbin/nginx -s reload

 

USER1: 备份日志文件使用的较多,比如备份以前的日志文件,然后重新建立新的文件,需要使得这个新的文件生效

kill -USER1 pid('cat /logs/nginx.pid') 等价于 ./sbin/nginx -s reopen

./sbin/nginx -t 检测日志文件是否有问题

NGINX 配置:

//全局区域:

worker process 1: 只有一个工作进程。一般设置cpu数*核数 4*8=32

events {

//一般配置nginx的连接的特性,如一个worker可以产生多少连接
    worker_connections  1024;//一个子进程最大允许1024个连接
}

/**配置http服务器的区域*/

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;

//基于域名
   server1{//虚拟主机区域

listen:80

server_name nicky.com

       Location{

           定位。把特殊的路径和文件再次定位,如image目录单独处理

          php 单独处理等

         root /html/ //可以是相对和绝对路径

         index index.html

      }

   }

//基于端口

   server1{

      listen 1988;

      server_name nicky.com;

       location{

          root /html/nicky //可以是相对和绝对路径

           index nicky.html

      }

   }

//基于ip

   server1{

      listen 80;

      server_name 192.168.1.133

       location{

          root /html/ip //可以是相对和绝对路径

           index nicky.html

      }

   }


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

日志管理:

Nginx允许针对不同的server 做不同的log

1 日志格式 指那些记录选项

默认的日志格式:main

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

即 :

用户访问ip-远程用户[访问时间] 请求方法 请求body 长度 refer 来源信息

$http_referer:上一个页面自来哪里

http-yser-agent:用户代理 蜘蛛被转发的请求的原始ip

http_x_forwarded_for: 在经过代理时,代理把你的本来的ip加在刺头的信息中传输你原始的IP


 #access_log  logs/access.log  main;
该主机 access_log 文件位于logs/access.log 使用的格式是 main 格式

除了main格式 你可以自定义其他格式

猜你喜欢

转载自nicky19870612.iteye.com/blog/2199439