Nginx1.2.3入門指南

jopen 12年前發布 | 41K 次閱讀 Nginx Web服務器

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


然后重新啟動。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!