Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603382
  • 博文数量: 244
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-27 09:53
个人简介

记录学习,记录成长

文章分类

全部博文(244)

我的朋友

分类: LINUX

2016-03-09 12:29:14

nginx的安装及简单配置

一.安装Nginx
1.一些准备工作
1.1 安装编译Nginx所需要的某些包
[root@localhost ~]# yum install pcre-devel zlib-devel openssl-devel -y
Nginx的gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl库;

1.2 提供一个运行nginx进程的用户和组
[root@localhost ~]# groupadd -r nginx
[root@localhost ~]# useradd -g nginx -s /sbin/nologin -r nginx
[root@localhost ~]# id nginx
uid=496(nginx) gid=493(nginx) groups=493(nginx)

2.下载Nginx安装包
源码包下载地址:
RPM包下载地址:

这里使用nginx-1.8.1源码包

3.解压缩并编译
[root@localhost ~]# tar xf nginx-1.8.1.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.8.1/

./configure \
> --prefix=/etc/nginx \
> --prefix=/etc/nginx \
> --sbin-path=/usr/sbin/nginx \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --pid-path=/var/run/nginx/nginx.pid \
> --lock-path=/var/lock/nginx.lock \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --http-client-body-temp-path=/var/tmp/nginx/client/ \
> --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
> --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
> --http-scgi-temp-path=/var/tmp/nginx/scgi \
> --with-pcre

[root@localhost nginx-1.8.1]# make && make install

[root@localhost nginx-1.8.1]# ls /etc/nginx/
fastcgi.conf            html        mime.types.default  scgi_params.default
fastcgi.conf.default    koi-utf     nginx.conf          uwsgi_params
fastcgi_params          koi-win     nginx.conf.default  uwsgi_params.default
fastcgi_params.default  mime.types  scgi_params         win-utf

4.启动Nginx
首先确保本地的80端口未被占用,然后启动
[root@localhost nginx]# pwd
/etc/nginx
[root@localhost nginx]# nginx -c ./nginx.conf
如果出现错误,缺少包就去安装,无对应目录就去创建即可,如
[root@localhost nginx]# nginx -c ./nginx.conf
nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)
当创建好对应的目录后,再次nginx -c ./nginx.conf没有出现信息,即说明安装好了;
[root@localhost nginx]# netstat -untlp | grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      27325/nginx   

5.Nginx的控制
格式:nginx -s signal

其中signal可为:
stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
reopen — reopening the log files
当然也可以写一个启动脚本来控制Nginx服务

6.访问Nginx

显示欢迎页面,说明Nginx已经安装成功


二.Nginx的简单配置
1.配置文件说明
Nginx配置文件主要分为四个部分:全局配置main,主机设置server,负载均衡服务器设置upstream和URI匹配特定位置设置location。
main部分设置的命令将影响其他所有设置;server部分的命令主要用于设置虚拟主机;upstream命令主要用于负载均衡,设置一系列的后端服务器;location部分用于匹配网页位置;四者之间的关系为:server继承main,location继承server,upstream既不会继承其他设置也不会被继承


2.修改配置文件/etc/nginx/nginx.conf(最好先备份)
[root@localhost nginx]# cat nginx.conf
user  nginx;                               #主模块命令,指定Nginx的worker进程运行用户及组;
worker_processes  1;                 #指定Nginx要开启的进程数,一般为CPU的总核数或总核数的两倍;

#error_log  logs/error.log;          #错误日志的存放路径,级别可为:debug info notice warn error crit
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;              #指定pid存放的路径;




events {
use  epoll;                                    
#使用的网络I/O模型可为select epoll poll kqueue rtsig和/dev/poll
#其中select和poll是标准的工作模式,kqueue和epoll是高效的工作模式
#建议Linux系统使用epoll模式,FreeBSD系统使用kqueue模式;

worker_connections  2048;             
#定义Nginx每个进程的最大连接数,默认1024,最大客户端连接数由
#worker_connections和worker_processes决定,进程的最大连接数受
#Linux系统进程的最大打开文件数限制,执行ulimit -n 2048后
#该选项的设置才能生效;
}


HTTP服务器属性配置
http {
    include       mime.types;             #主模块命令,实现对配置文件所包含文件的设定,可减少复杂度;

    default_type  application/octet-stream;
#HTTP核心模块命令,默认类型为二进制流,也就是当文件类型未定义时使用这种方式,例如,没有配置PHP环境时,Nginx是不与解析的,此时浏览器访问PHP文件就会出现下载窗口;


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
#定义Nginx日志的输出格式,main为此日志格式的名称,可以在access_log命令中引用;
   
    sendfile        on;
#用于开启高效文件传输模式,将tcp_nopush和tcp_nodelay两个命令设置为on用于防止网络阻塞;

    #tcp_nopush     on;

    keepalive_timeout  65;
#设置客户端连接保持活动的超时时间,超出该时间后,服务器会关闭该连接;

    #gzip  on;
#用于设置gzip模块的开启和关闭,开启gzip压缩表示实时压缩输出数据流;

虚拟主机设置
    server {
        listen       80;                         #指定虚拟主机的服务端口
        server_name  localhost;          #指定IP地址或域名,多个之间用空格分开


        #charset koi8-r;                      #设置使用的字符集




        location / {                              #设置URL地址匹配
            root   html;                         #设置虚拟主机的网页根目录,可为相对或绝对路径
            index  index.html;                #设定访问的默认首页地址
#access_log  logs/host.access.log  main;     #此虚拟主机的访问日志路径
        }
#设置这个location后,当访问时,会到安装目录下的html目录中找到index.html返回给客户端;


        location /doc/ {
            root   html;
            index  index.html;
        }
#设置这个location后,当访问/doc时,会到安装路径下的html目录下的doc目录中找到index.html返回;


        error_page  404              /404.html;              #虚拟主机的错误信息返回页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
注意:这些错误信息返回页面的大小一定要超过512KB,否则会被IE浏览器替换为IE的默认错误页面;
}

3.创建配置文件所需要的目录和页面
[root@localhost html]# ll
total 16
-rw-r--r-- 1 root root  635 Mar  9 11:58 404.html         #大小要超过512K
-rw-r--r-- 1 root root  537 Mar  8 21:24 50x.html
drwxr-xr-x 2 root root 4096 Mar  9 10:59 doc
-rw-r--r-- 1 root root  612 Mar  8 21:24 index.html
[root@localhost html]# ll doc/
total 4
-rw-r--r-- 1 root root 13 Mar  9 10:59 index.html
[root@localhost html]# pwd
/etc/nginx/html

4.访问Nginx
访问不存在的页面

访问/doc



到这里已经完成了Nginx的安装以及简单配置,但是仍有许多功能未使用,等到以后用到哪个模块哪个指令时再做仔细的说明吧!


三.补充:
1.nginx命令的帮助信息
[root@localhost nginx]# nginx -h
nginx version: nginx/1.8.1
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /etc/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file


2.Nginx的模块说明
Nginx由内核和模块组成,而内核设计非常简洁,完成的工作仅仅是通过查找配置文件将客户端请求映射到一个匹配的location块,在这个location中所配置的每个命令将会启用不同的模块去完成相应的工作;


nginx的模块从结构上分为核心模块,基础模块和第三方模块,其中,HTTP模块,EVENT模块和MAIL模块等属于核心模块,HTTP Access模块,HTTP FastCGI模块,HTTP Proxy模块和HTTP Rewrite模块属于基本模块,而HTTP Upstream Request Hash模块,Notice模块和HTTP Access Key模块属于第三方模块,用于根据自己的需求开发的模块也都属于第三方模块;


Nginx的模块直接被编译进Nginx,因此属于静态编译方式。在启动Nginx后,自动加载Nginx的模块,不想Apache中一样,首先将模块编译为一个so文件,然后在配置文件中指定是否加载。而Nginx在解析配置文件时,每个模块都有可能去处理某个请求,但是同一个请求只能由一个模块来完成;


Nginx的核心模块默认是安装的,如果不想安装可以使用--without-xxx选项取消安装,可选模块默认是未安装的,如果想要安装可以使用--with-xxx选项来安装;


3.编译选项说明
编译选项的官方说明:

[root@localhost nginx-1.8.1]# ./configure --help
  --help                             print this message
  
  --prefix=PATH                      set installation prefix
  
  --sbin-path=PATH                   set nginx binary pathname
  
  --conf-path=PATH                   set nginx.conf pathname
  
  --error-log-path=PATH              set error log pathname
  
  --pid-path=PATH                    set nginx.pid pathname
  
  --lock-path=PATH                   set nginx.lock pathname


  --user=USER                        set non-privileged user for
                                     worker processes
 
  --group=GROUP                      set non-privileged group for
                                     worker processes


  --build=NAME                       set build name
  
  --builddir=DIR                     set build directory


  --with-rtsig_module                enable rtsig module
  
  --with-select_module               enable select module
  
  --without-select_module            disable select module
  
  --with-poll_module                 enable poll module
  
  --without-poll_module              disable poll module
  
  --with-threads                     enable thread pool support
  
  --with-file-aio                    enable file AIO support
  
  --with-ipv6                        enable IPv6 support
  
  --with-http_ssl_module             enable ngx_http_ssl_module
  
  --with-http_spdy_module            enable ngx_http_spdy_module
  
  --with-http_realip_module          enable ngx_http_realip_module
  
  --with-http_addition_module        enable ngx_http_addition_module
  
  --with-http_xslt_module            enable ngx_http_xslt_module
  
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  
  --with-http_geoip_module           enable ngx_http_geoip_module
  
  --with-http_sub_module             enable ngx_http_sub_module
  
  --with-http_dav_module             enable ngx_http_dav_module
  
  --with-http_flv_module             enable ngx_http_flv_module
  
  --with-http_mp4_module             enable ngx_http_mp4_module
  
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  
  --with-http_random_index_module    enable ngx_http_random_index_module
  
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  
  --with-http_degradation_module     enable ngx_http_degradation_module
  
  --with-http_stub_status_module     enable ngx_http_stub_status_module


  --without-http_charset_module      disable ngx_http_charset_module
  
  --without-http_gzip_module         disable ngx_http_gzip_module
  
  --without-http_ssi_module          disable ngx_http_ssi_module
  
  --without-http_userid_module       disable ngx_http_userid_module
  
  --without-http_access_module       disable ngx_http_access_module
  
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  
  --without-http_geo_module          disable ngx_http_geo_module
  
  --without-http_map_module          disable ngx_http_map_module
  
  --without-http_split_clients_module disable ngx_http_split_clients_module
  
  --without-http_referer_module      disable ngx_http_referer_module
  
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  
  --without-http_proxy_module        disable ngx_http_proxy_module
  
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  
  --without-http_scgi_module         disable ngx_http_scgi_module
  
  --without-http_memcached_module    disable ngx_http_memcached_module
  
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  
  --without-http_browser_module      disable ngx_http_browser_module
  
  --without-http_upstream_hash_module      disable ngx_http_upstream_hash_module
 
  --without-http_upstream_ip_hash_module    disable ngx_http_upstream_ip_hash_module
                                     
  --without-http_upstream_least_conn_module  disable ngx_http_upstream_least_conn_module
                                     
  --without-http_upstream_keepalive_module  disable ngx_http_upstream_keepalive_module
                                     


  --with-http_perl_module            enable ngx_http_perl_module
  
  --with-perl_modules_path=PATH      set Perl modules path
  
  --with-perl=PATH                   set perl binary pathname


  --http-log-path=PATH               set http access log pathname
  
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
 
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
 
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
 
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
 
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files


  --without-http                     disable HTTP server
  
  --without-http-cache               disable HTTP cache


  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  
  --without-mail_imap_module         disable ngx_mail_imap_module
  
  --without-mail_smtp_module         disable ngx_mail_smtp_module


  --with-google_perftools_module     enable ngx_google_perftools_module
  
  --with-cpp_test_module             enable ngx_cpp_test_module


  --add-module=PATH                  enable an external module


  --with-cc=PATH                     set C compiler pathname
  
  --with-cpp=PATH                    set C preprocessor pathname
  
  --with-cc-opt=OPTIONS              set additional C compiler options
  
  --with-ld-opt=OPTIONS              set additional linker options
  
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64


  --without-pcre                     disable PCRE library usage
  
  --with-pcre                        force PCRE library usage
  
  --with-pcre=DIR                    set path to PCRE library sources
  
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  
  --with-pcre-jit                    build PCRE with JIT compilation support


  --with-md5=DIR                     set path to md5 library sources
  
  --with-md5-opt=OPTIONS             set additional build options for md5
  
  --with-md5-asm                     use md5 assembler sources


  --with-sha1=DIR                    set path to sha1 library sources
  
  --with-sha1-opt=OPTIONS            set additional build options for sha1
  
  --with-sha1-asm                    use sha1 assembler sources


  --with-zlib=DIR                    set path to zlib library sources
  
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro


  --with-libatomic                   force libatomic_ops library usage
  
  --with-libatomic=DIR               set path to libatomic_ops library sources


  --with-openssl=DIR                 set path to OpenSSL library sources
 
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL


  --with-debug                       enable debug logging
  
  
参考资料:


http://my.oschina.net/liucao/blog/470241?fromerr=fNOjNiBh
实战Nginx[张宴著]

阅读(1458) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~