好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

centos 安装 nginx 权威教程

1 yum -y install gcc gcc-c++ autoconf automake

 

 

 

2 yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

 

 

 

3 wget http://nginx.org/download/nginx-0.8.55.tar.gz

 

  tar -zxvf nginx-0.8.55.tar.gz

 

  cd nginx-0.8.55

 

  ./configure

 

  make 

 

  make install 

 

 

 

4 启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

5 查看nginx进程号

ps -ef |grep nginx

 

root      6971     1  0 14:19 ?        0 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nobody    6972  6971  0 14:19 ?        0 nginx: worker process

 

(master process 为主进程号

 worker process 为次进程号)

 

6 停止nginx

平滑的停止

Kill -9 '/usr/local/nginx/logs/nginx.pid'

 

从容的停止

kill - QUIT Nginx 6971

 

7 重启nginx

重启之前检查配置文件是否正确

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

 

没有问题的话出现的是这个

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

重启nginx

kill -HUB '/usr/local/nginx/logs/nginx.pid'

 

8 nginx配置文件

  

 

user  nobody;              ------------>使用的用户和组

worker_processes  4;         ------------>指定工作衍生的进程数(一般等于CPU总核数或2倍,如cpu是4核的,则worker_processes为8)

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

error_log   /var/log/nginx/error.log crit

#error_log  logs/error.log  info;            ------>错误日志路径 和 日志级别 (debug info notice warn error crit)

 

#pid        logs/nginx.pid;             ---------->指定pid文件位置

pid         /usr/local/nginx/logs/nginx.pid

 

worker_rlimit_nofile 51200         ------> 指定文件描述符数量 51200

 

 

events {

         use epoll;            ---->使用的网络I/O模型 linux系统推荐采用epoll模型,freeBSD系统推荐采用kqueue模型 

 

    worker_connections  51200;      ------------>运行的连接数

}

 

 

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;

    client_max_body_size 8m;          ----------->设置客户端能够上传的文件大小

 

    sendfile        on;

    tcp_nopush     on;

    tcp_nodelay    on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

 

 

   fastcgi_connect_timeout 300;

   fastcgi_send_timeout  300;

   fastcgi_read_timeout  300;

   fastcgi_buffer_size 64k;

   fastcgi_buffer 4 64k;

   fastcgi_busy_buffers_size 128k;

   fastcgi_temp_file_write_size 128k;

 

 

   

 

    gzip  on;     ----------->开启gzip压缩

    gzip_min_length 1k;

    gzip_buffers  4 16k;

    gzip_http_version 1.1;

    gzip_comp_level 2;

    gzip_types    text/plain application/x-javascript  text/css  application/xml;

    gzip_vary on;

 

 #limit_zone crawler $binary_remote_ddr 10m;

 

 

 

 

    server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  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;

        #}

    }

 

 

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

    #    server_name  localhost;

 

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_timeout  5m;

 

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    #    ssl_prefer_server_ciphers   on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

}

 

 

 

 

9 虚拟主机 

 

nginx配置基于IP的虚拟主机

 添加虚拟接口:

 ifconfig eht0:1 192.168.1.130 netmask 255.255.255.0 

 ifconfig eth0:2 192.168.1.131 netmask 255.255.255.0 

 

 查看

 ifconfig

 

 

 vim /usr/local/nginx/conf/nginx.conf添加如下内容:

 

 http

  {

    #第1关虚拟主机

    server

    {

       listen 192.168.1.131:80

       server_name 192.168.1.131

       access_log  logs/192.168.1.131.access.log combined;

       location    /

      {

        index  index.html  index.htm index.php; 

        root    /home/xudong/html;

      }

    }

 

 

    #第2关虚拟主机

    server

    {

       listen 192.168.1.132:80

       server_name 192.168.1.132

       access_log  logs/192.168.1.132.access.log combined;

       location    /

      {

        index  index.html  index.htm index.php; 

        root    /home/xudong/html;

      }

    }

 

    #第3关虚拟主机

    server

    {

       listen 192.168.1.133:80;

       server_name 192.168.1.133;

       access_log  logs/192.168.1.133access.log combined;

       location    /

      {

        index  index.html  index.htm index.php; 

        root    /home/xudong/html;

      }

    }

 

 

 

 }  

 

  

 

 

nginx 基于域名的虚拟主机

 

server

 {

    listen 80;

    server_name      www.rongrong.com;

    access_log       logs/rongrong.com.access.log combined;

    location         /

     {

       index        index.html  index.htm  index.php;

       root         /home/rongrong/pub_html;

     }

 }

 

 

 

server

 {

    listen 80;

    server_name      www.wangjuan.com;

    access_log       logs/wangjuan.com.access.log combined;

    location         /

     {

       index        index.html  index.htm  index.php;

       root         /home/wangjuan/pub_html;

     }

 }

 

 

 

 

10 设定nginx的日志格式

   log_format combined '$remote_addr - $remote_user [$time_local] '

                       '"$request" $status $body_bytes_sent '

                       ' "$http_referer" "$http_user_agent"'; 

 

  

   如果经过了反向代理则设置 日志格式 

   log_format mycombined '$http_x_forwarded_for - $remote_user [$time_local] '

                       '"$request" $status $body_bytes_sent '

                       ' "$http_referer" "$http_user_agent"'; 

 

 

  (1)关闭日志记录

     access_log off;

  (2)使用默认的日志格式记录;

     access_log /var/log/nginx/rongrong.log combined buffer=32k;

  (3)使用自定义的格式的日志记录

       log_format mycombined '$http_x_forwarded_for - $remote_user [$time_local] '

                       '"$request" $status $body_bytes_sent '

                       ' "$http_referer" "$http_user_agent"'; 

  

        access_log /var/log/nginx/access.log mycombined buffer=32k;

                                  $server_name.log

 

11 nginx切割日志文件

 

 vim /usr/local/nginx/sbin/cu_ngxin_log.sh 内容如下:

 

 #!/bin/bash

 #这个脚本每天00:00运行

 logs_path="/data1/logs/"

 

 mkdir -p ${logs_path}$(date -d "yesterday"   +"%Y") /$(date -d "yesterday" +"%m") /

 mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%y") /$(date -d "yesterday" +"%m") /

 access_$(date -d "yesterday" +"%Y%m%d").log

 kill -USR1 'cat /usr/loca/nginx/logs/nginx.pid'

 

 

 

 

crontab -e 

 

00 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh

 

 

 

12 nginx的压缩输出配置

 

 

 

 

 

 

 

 

 

 

 

 

 

13 nginx的自动列目录配置

 

  

 server

 {

    listen 80;

    server_name      www.wangjuan.com;

    access_log       logs/wangjuan.com.access.log combined;

    location         /

      autoindex on;          ---------------->自动列出目录

      autoindex_exact_size on/off      ----------->设定索引文件大小的单位(B KB MB GB)

      autoindex_localtime  on/off       ----------->开启本地时间来显示文件时间的功能

     {

       index        index.html  index.htm  index.php;

       root         /home/wangjuan/pub_html;

     }

 }

  

 

 

 

 

 

 

14 nginx浏览器本地缓存设置

 

对常见的格式的图片 flash文件则浏览器本地缓存30天,对js,css文件则浏览器本地缓存1小时 代码如下

 

 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

   {

        expires  30d;

   }

 

 location ~ .*\.(js|css)?$

   {

        expires  1h;

 

   } 

 

 

 

15 安装LNMP平台

 

yum -y install gcc gcc-c++ autoconf  libjpeg libjpeg-devel  libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2  glib2-devel bzip2 bzip2-devel 

ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

 

 

16  

 

 

查看更多关于centos 安装 nginx 权威教程的详细内容...

  阅读:55次