天天看點

nginx 安裝配置指南

<a href="http://s3.51cto.com/wyfs02/M02/74/5E/wKiom1YbVAHxXGywAAGjQDFA6PQ913.jpg" target="_blank"></a>

yum install gccgcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel make–y

建立nginx的系統使用者與組(-r :建立指定系統中的使用者、組)

groupadd -r nginx

useradd -s /sbin/nologin -g nginx -r nginx

驗證

id nginx

配置nginx的子產品

./configure --prefix=/usr \

--sbin-path=/usr/sbin/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--user=nginx \

--group=nginx \

 --with-http_ssl_module \

--with-http_flv_module \

--with-http_gzip_static_module \

--http-log-path=/var/log/nginx/access.log \

--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 \

--with-http_addition_module \

--with-http_stub_status_module \

--with-http_gunzip_module \

--with-http_auth_request_module \

--with-http_secure_link_module \

--with-http_realip_module 

make &amp;&amp; make install

啟動nginx

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

檢視服務是否啟動

ps -ef | grep nginx

netstat -antulp | grep nginx

啟動的時候會報錯,是因為剛才自己手動指定了臨時目錄,是以這時你要自己手動建立一個這個目錄即可。

mkdir /var/tmp/nginx/client -pv

nginx -c /etc/nginx/nginx.conf

停止nginx

從容關閉QUIT

kill -QUIT $(cat /var/run/nginx/nginx.pid)

快速停止

kill -TERM $(cat /var/run/nginx/nginx.pid)

強制結束所有的nginx程序

kill -9 nginx

平滑重新開機nginx

kill -HUP $(cat /var/run/nginx/nginx.pid)

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

可以把rpm包中的nginx腳本檔案複制過來自己修改後再用

 rpm2cpio nginx-1.9.0.rpm | cpio -div  解壓rpm包

cp nginx /etc/init.d/

chmod a+x /etc/init.d/nginx

chkconfig --add nginx

chkconfig --list nginx

修改/etc/init.d/nginx腳本檔案,否則服務無法用這個腳本啟動,因為在安裝的過程中,這些都是自己指定的檔案和路徑。(已修改)

prog=nginx

nginx=${nginx-/usr/sbin/nginx}

conffile=${CONFFILE-/etc/nginx/nginx.conf}

lockfile=${LOCKFILE-/var/lock/nginx.lock}

pidfile=${PIDFILE-/var/run/nginx/nginx.pid}

SLEEPMSEC=100000

RETVAL=0

編譯nginx.conf 詳解

worker_processes 2  這裡建議有幾個cpu就分給它幾個程序即可。

worker_connections 1024;設定可以最大的連接配接請求數(預設1024),超過過就在隊列裡等待

sendfile  on ;  記憶體分為兩段,一段椒user,另一段是kernel,而使用者去通路請求時,就會去通路核心,而這個是把通路的這個過程記錄下來,放入記憶體中,而不用每次去核心中讀取了。頻繁通路東西都會放在記憶體當中。(是否開啟帶有緩存的這種機制)

gzip on ;  是否啟動壓縮功能,在大并發的時候會有所展現。

Location類似于apache的directory,而root旁沒寫絕對路徑那麼它的根就是你安裝時指定的那個路徑。

<a href="http://s3.51cto.com/wyfs02/M01/74/5A/wKioL1YbXhGScjaUAACJnsQQyB8412.jpg" target="_blank"></a>

而這裡的/(根)是從/usr/html這裡開始的,下面有定義。(從安裝路徑的html開始)

<a href="http://s3.51cto.com/wyfs02/M01/74/5E/wKiom1YbXkCilicbAAB5r-YWBas828.jpg" target="_blank"></a>

虛拟主機的配置過程。

我在一個主機裡用一個Ip對應多個域名來配置,寫在/etc/hosts裡

<a href="http://s3.51cto.com/wyfs02/M02/74/5E/wKiom1YbXoaCCUPcAABMW-2-Z_U051.jpg" target="_blank"></a>

1、虛拟主機配置過程,/etc/nginx/nginx.conf

<a href="http://s3.51cto.com/wyfs02/M01/74/5E/wKiom1YbXr6iVKTXAADALZ4jAQA871.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/74/5E/wKiom1YbXuej0Xb8AAEn3QZLK8I445.jpg" target="_blank"></a>

最後:killall -1 nginx (與service nginx reload結果一樣)

建立目錄與在目錄中寫入内容,指令為index.html

mkdir –p/var/www/virtual2

mkdir –p /var/www/virtual

驗證:

<a href="http://s3.51cto.com/wyfs02/M01/74/5B/wKioL1YbXzrxHUG9AACt9KusK9Y463.jpg" target="_blank"></a>

下面配置檔案中會用到大量的正規表達式。

<a href="http://s3.51cto.com/wyfs02/M00/74/5B/wKioL1YbX3nTbOHdAAF-kHyJVeQ176.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/74/5E/wKiom1YbX5bA5YObAAKP8P5Tctk488.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M02/74/5B/wKioL1YbX_jDNDUOAAGfQAx4ydo294.jpg" target="_blank"></a>

把這個打開

<a href="http://s3.51cto.com/wyfs02/M02/74/5B/wKioL1YbYE3SieRBAAD3szZLENQ848.jpg" target="_blank"></a>

以這個為例,再繼續修改配置一下。(warn是日志錯誤級别)

tailf  /var/log/nginx/www.example.com-error.log

<a href="http://s3.51cto.com/wyfs02/M00/74/5B/wKioL1YbYMbhH3jFAAHDccEsBpA857.jpg" target="_blank"></a>

403錯誤:索引檔案未找到

編輯/etc/nginx/nginx.conf

mkdir /var/www/error

cd /var/www/error

echo This is error &gt; 40x.html

接着重新開機服務并驗證結果:

<a href="http://s3.51cto.com/wyfs02/M01/74/5E/wKiom1YbYcugbd-wAAGj99PT8O0000.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M01/74/5B/wKioL1YbYouhJKIbAAGIUACjxPk178.jpg" target="_blank"></a>

首先:還是繼續修改我們的這個配置檔案,把我們原有的再進行一下改造即可。

<a href="http://s3.51cto.com/wyfs02/M00/74/5B/wKioL1YbYrahxQdxAAG0lw2Hqv4606.jpg" target="_blank"></a>

服務重新開機并驗證結果

<a href="http://s3.51cto.com/wyfs02/M02/74/5E/wKiom1YbYuSh6ujOAAD2sV1aLHs750.jpg" target="_blank"></a>

之前我複制過來的這個檔案

1、編輯/etc/nginx/nginx.conf 配置檔案。

<a href="http://s3.51cto.com/wyfs02/M02/74/5B/wKioL1YbY0qjaQ4CAAIEIant_wA637.jpg" target="_blank"></a>

2、建立目錄及索引檔案

<a href="http://s3.51cto.com/wyfs02/M01/74/5B/wKioL1YbY32RchRkAACmpoKcavo721.jpg" target="_blank"></a>

3、重新啟動服務

<a href="http://s3.51cto.com/wyfs02/M02/74/5B/wKioL1YbY6uDchXiAAD5uOS68_U737.jpg" target="_blank"></a>

4、驗證結果:

<a href="http://s3.51cto.com/wyfs02/M01/74/5E/wKiom1YbY86Czat5AABXk-2mBkw052.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/74/5B/wKioL1YbZEGhL7jjAAGKJTKvRnk444.jpg" target="_blank"></a>

沒有明确拒絕都是允許所有

<a href="http://s3.51cto.com/wyfs02/M02/74/5E/wKiom1YbZFiQL922AAKhLByajjY667.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/74/5B/wKioL1YbZJqgSxZdAAD4Vk_KIlY110.jpg" target="_blank"></a>

編譯安裝的時候必須指定這個子產品,否則是沒有這個功能的。

--with-http_stub_status_module

1、編輯 /etc/nginx/nginx.conf

<a href="http://s3.51cto.com/wyfs02/M00/74/5B/wKioL1YbZR7hkru8AAJKLM36lbE743.jpg" target="_blank"></a>

2、重新啟動服務

<a href="http://s3.51cto.com/wyfs02/M01/74/5B/wKioL1YbZU3QmKfSAADz0woGJYI220.jpg" target="_blank"></a>

3、驗證結果:

<a href="http://s3.51cto.com/wyfs02/M00/74/5B/wKioL1YbZXvjZwCVAADfQFUaCjw901.jpg" target="_blank"></a>

這個nginx_status目錄之前是不用建立的,按以上操作完成即可。還可以對這個目錄進行加密,不讓其他人看到這個結果,這個是可以傳給類似ngios與cacti來作監控用的一個功能。

<a href="http://s3.51cto.com/wyfs02/M01/74/5C/wKioL1YbaFLC8xmrAAFJbsXF0tA382.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M01/74/5F/wKiom1YbaFmANEbkAAFTkvx9jjc269.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M02/74/5F/wKiom1YbaG_R393rAANHstMyF0E385.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/74/5C/wKioL1YbaObyTkLnAAHAJqaBs70575.jpg" target="_blank"></a>

nginx.conf 配置

user  wwwroot;

worker_processes  auto;

worker_rlimit_nofile 65535;

events {

    use epoll;

    worker_connections  65535;

}

http {

    server_tokens off;

    include       mime.types;

    default_type  application/octet-stream;

    server_names_hash_bucket_size 1024;

    variables_hash_max_size 1024;

    log_format main  '$remote_addr - $remote_user [$time_local] "$request" '

                 '$status $body_bytes_sent "$http_referer" '

                 '"$http_user_agent" $http_x_forwarded_for';

    sendfile   on;

    tcp_nopush on;

    tcp_nodelay on;

    keepalive_timeout  120;

    client_max_body_size 50m;

    gzip  on;

    gzip_vary on;

    gzip_min_length 10240;

    gzip_comp_level 9;

    gzip_proxied expired no-cache no-store private auth;

    gzip_types text/plain text/xml text/css text/comma-separated-values

               text/javascript application/x-javascript application/atom+xml

               image/jpeg image/gif image/png image/jpg;

    gzip_disable "MSIE [1-6]\.";

    #open_file_cache          max=102400 inactive=20s;

    #open_file_cache_valid    60s;

    #open_file_cache_min_uses 5;

    #open_file_cache_errors   off;

    fastcgi_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=DEDECMS:500m inactive=60m;

    fastcgi_cache_key "$scheme$request_method$host$request_uri";

    geoip_country /usr/local/nginx/conf/GeoIP.dat;

    fastcgi_connect_timeout 3600;

    fastcgi_send_timeout 3600;

    fastcgi_read_timeout 3600;

    fastcgi_intercept_errors on;

    server {

        listen       80;

        server_name  localhost;

        return 500;

        location / {

            root   html;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

    }

        server_name  .example.com;

        access_log  logs/example.access.log  main;

    include yw.conf;

nginx 位址重寫規則 泛解析

##start .example.com##

        server_name_in_redirect off;

        access_log logs/www.example.com.access.log main;

        error_log logs/www.example.com.error.log;

        include gen.conf;

        if ($host = 'example.com' ) {

            rewrite ^/(.*)$ http://www.example.com/$1 permanent;

        if ($host ~* (\b(?!www\b)[a-zA-Z0-9\-]+).example.com$) {

           set $rs1 /$1;

        set $rootdir /alidata1/web/html/www.example.com$rs1;

                root  $rootdir;

            root  $rootdir;

            expires 1M;

            add_header Cache-Control "public";

            index index.html index.htm index.html index.php default.html Default.html;

        location ~ .*\.(php|php5)?$ {

                    root  $rootdir;

                    fastcgi_pass   127.0.0.1:9000;

                    fastcgi_index  index.php;

                    fastcgi_param  SCRIPT_FILENAME  $rootdir$fastcgi_script_name;

                    include        fastcgi_params;

  error_page  404              /404.html;

        location = /404.html {

            root  /alidata1/web/html/www.example.com;

##end .example.com##

沒有位址重寫

##start www.example.com##

        server_name  www.example.com;

        set $rootdir /home/wwwroot/www.example.com;

        error_page  404              /404.html;

            root  /home/wwwroot/www.example.com;

##end example.com##

全部反向代理 nginx.conf

upstream proxyserver {

    server 114.80.80.41:80 fail_timeout=1s max_fails=1;

    server 103.6.222.85:80 backup;

    keepalive 20;

include proxy.conf;

proxy.conf

server {

   server_name  ~^([^\.]+)\.([^\.]+)$ ~^([^\.]+)\.([^\.]+)\.([^\.]+)$  ~^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$;

   server_name_in_redirect off;

   access_log  logs/access.all.log main;

   error_log   logs/error.all.log;

   location / {

    proxy_pass         http://114.80.80.41;

    proxy_redirect     off;

    proxy_set_header   Host             $host;

    proxy_set_header   X-Real-IP        $remote_addr;

    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    add_header X-Cache $upstream_cache_status;

    expires 1M;

    client_max_body_size       10m;

    client_body_buffer_size    128k;

    proxy_connect_timeout      90;

    proxy_send_timeout         90;

    proxy_read_timeout         90;

    proxy_buffer_size          4k;

    proxy_buffers              4 32k;

    proxy_busy_buffers_size    64k;

    proxy_temp_file_write_size 64k;

   server_name  google.com www.google.com g.cn www.g.cn;

   access_log  logs/g.all.log main;

   error_log   logs/g.all.log;

    proxy_pass         https://www.google.com/?gws_rd=ssl;

本文轉自 a120518129 51CTO部落格,原文連結:http://blog.51cto.com/silencezone/1702114,如需轉載請自行聯系原作者

繼續閱讀