Nginx1.2.3入门指南

jopen 12年前

http://wiki.nginx.org/GettingStarted

 

Nginx1.2.3入门指南

Nginx is a reverse proxy first and HTTP server second.

Nginx首先是反向代理,其次是HTTP服务器。

 

  1. 源码编译安装过程
  2. 进入Nginx的解压缩目录

Nginx的安装需要如下第三方包:

  • pcreThe PCRE library is a setof functions that implement regularexpression pattern matchingusing the same syntax and semantics asPerl 5.www.pcre.org
  • zlibThe zlib  library is designed to be afree, general-purpose, legally unencumbered -- that is, not covered by anypatents -- lossless data-compressionlibraryfor use on virtually any computer hardware and operating system.www.zlib.net
  • openssl

如果缺少上述包,会提示错误。

 

  1. 下载源代码编译的过程如下:

执行./configure,提示如下错误:

./configure: error: the HTTP rewrite module requires the PCRElibrary.

You can either disable the module by using--without-http_rewrite_module

option, or install the PCRE library into the system, or buildthe PCRE library

statically from the source with nginx by using --with-pcre=<path>option.

 

安装PCRE

sudo apt-get install libpcre3-dev

 

再次执行./configure,提示如下错误:

./configure: error: the HTTP gzip module requires the zliblibrary.

You can either disable the module by using--without-http_gzip_module

option, or install the zlib library into the system, or buildthe zlib library

statically from the source with nginx by using--with-zlib=<path> option.

 

安装zlib

sudo apt-get install zlib1g-dev

 

再次执行./configure,提示如下错误:

./configure: error: the HTTP cache module requires md5 functions

from OpenSSL library. You can either disable the module by using

–without-http-cache option, or install the OpenSSL library intothe system,

or build the OpenSSL library statically from the source withnginx by using

–with-http_ssl_module –with-openssl= options.

 

安装OpenSSL

sudo apt-get install openssl

sudo apt-get install libssl-dev

 

再次执行./configure成功,提示如下:

Configuration summary

  + using system PCRElibrary

  + OpenSSL library is notused

  + using builtin md5 code

  + sha1 library is notfound

  + using system zliblibrary

 

  nginx path prefix:"/usr/local/nginx"

  nginx binary file:"/usr/local/nginx/sbin/nginx"

  nginx configurationprefix: "/usr/local/nginx/conf"

  nginx configurationfile: "/usr/local/nginx/conf/nginx.conf"

  nginx pid file:"/usr/local/nginx/logs/nginx.pid"

  nginx error log file:"/usr/local/nginx/logs/error.log"

  nginx http access logfile: "/usr/local/nginx/logs/access.log"

  nginx http clientrequest body temporary files: "client_body_temp"

  nginx http proxytemporary files: "proxy_temp"

  nginx http fastcgitemporary files: "fastcgi_temp"

  nginx http uwsgitemporary files: "uwsgi_temp"

  nginx http scgitemporary files: "scgi_temp"

 

执行make

 

执行sudo makeinstall

 

  1. 默认安装在/usr/local/nginx,包括如下文件:

hanxb@ubuntu :/usr/local/nginx$ ll

total 24

drwxr-xr-x  6 root root4096 2012-09-17 02:12 ./

drwxr-xr-x 11 root root 4096 2012-09-17 02:12 ../

drwxr-xr-x  2 root root4096 2012-09-17 02:12 conf/

drwxr-xr-x  2 root root4096 2012-09-17 02:12 html/

drwxr-xr-x  2 root root4096 2012-09-17 02:12 logs/

drwxr-xr-x  2 root root4096 2012-09-17 02:12 sbin/

 

 

  1. 启动Nginx

进入sbin目录,Ubuntu下直接执行其中的可执行文件Nginx,提示如下:

hanxb@ubuntu :/usr/local/nginx/sbin$ sudo ./nginx

 

启动结果如图:

 

具体的启动参数如下:

启动参数

含义

-c </path/to/config> 

Specify which configuration file Nginx should use instead of the default.

-g 

Set global directives. (version >=0.7.4)

-t 

Don't run, just test the configuration file. Nginx checks configuration for correct syntax and then try to open files referred in configuration.

-s signal 

Send signal to a master process: stopquitreopenreload. (version >= 0.7.53)

-v 

Print version.

-V 

Print nginx version, compiler version and configure parameters.

-p prefix 

Set prefix path (default: /usr/local/nginx/). (version >= 0.7.53)

-h,-? 

Print help.

如:/usr/local/nginx-t -c~/mynginx.conf -g"pid /var/run/nginx.pid;worker_processes 2;"

 

打开浏览器访问http://localhost/Nginx默认使用80端口。出现如下页面:

 

 

  • Nginx的配置

The Nginx configuration file is an inheriting-hierarchy, directives specified in a higher block willfilter down to lower blocks as a default value. So we should specify things inthe top most hierarchy whenever possible. 3 hierarchies are usually referred toas blocks: 

  • the HTTP-block
  • the server-block,which is what in Apache would be considered a virtual host.
  • and the location block,usually referrers to the URI. locationswork on the URI without any query parameters and only one location block willever be run. 

The hierarchy goes like this: http -> server ->location.

There are two other special locations (contain only a minoramount of directives), an event blockand the rootwhich the event blockand the http block reside in. 

 

Nginx的配置文件为/usr/local/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  1024;

}

 

 

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;

 

    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  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

}

 

Nginx will always choose the mostspecific match. Adding the default_serverflag to the listen directive, you can create a default virtual host to catchall domains without a proper match.

server {

  listen          80 default_server;

  server_name     _;

 

  index           index.html;

  root            /var/www/default;

}

server_name _;meansnothing and does nothing.

 

The requests for /forum aretransferred to new subdomain (forum.domain.com), while requests to files not in/forum will be served from /home/domain.com.

server {

  listen          80 default_server;

  server_name     www.domain.com;

 

  root            /home/domain.com;

 

  # This will match any URI beginning with /forum

  location /forum {

    # We capture the URI and redirect it to the subdomain.

    rewrite forum(.*) http://forum.domain.com$1 permanent;

  }

}

 

server {

  listen          80;

  server_name     forum.domain.com;

 

  index           index.php;

  root            /home/domain.com/forum;

}

 

修改配置文件后,需要停止当前运行的Nginx主进程,Ubuntu命令如下:

kill -HUPcat/var/run/nginx.pid

 

Nginx的主进程(master process)能够接收的信号如下:

TERM, INT

Quick shutdown

QUIT

Graceful shutdown

KILL

Halts a stubborn process

HUP

Configuration reload
Start the new worker processes with a new configuration
Gracefully shutdown the old worker processes

USR1

Reopen the log files

USR2

Upgrade Executable on the fly

WINCH

Gracefully shutdown the worker processes

 

Nginx的工作进程work process能够接收的信号如下:

TERM, INT

Quick shutdown

QUIT

Graceful shutdown

USR1

Reopen the log files


然后重新启动。