centos下nginx安裝與配置

nginx依賴如下模塊:html

l  gzip模塊須要 zlib 庫node

l  rewrite模塊須要 pcre 庫nginx

l  ssl 功能須要openssl庫git

tar xzvf nginx-1.9.15.tar.gzgithub

yum -y install pcre-devel openssl openssl-develweb

yum -y install zlib後端

#/usr/sbin/groupadd -f www
#/usr/sbin/useradd -g www wwwtomcat

不然啓動時會出錯「nginx: [emerg] getpwnam(「www」) failed」服務器

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module架構

注意,若是要訪問環境變量,lua方式必須安裝模塊ngx_lua modulengx_devel_kit module ,perl方式必須安裝 ngx_http_perl_module module

make

make install

二、啓動

首先,檢查下端口是否被佔用:lsof -i:80

[root@iZ23nn1p4mjZ ~]# cd /usr/local/nginx/sbin/
[root@iZ23nn1p4mjZ sbin]# ./nginx
[root@iZ23nn1p4mjZ sbin]# ps axu | grep nginx
root 27514 0.0 0.0 24428 828 ? Ss 10:30 0:00 nginx: master process ./nginx
nobody 27515 0.0 0.0 24848 1416 ? S 10:30 0:00 nginx: worker process
root 27531 0.0 0.0 103256 876 pts/0 S+ 10:30 0:00 grep nginx

三、中止

[root@iZ23nn1p4mjZ sbin]# ./nginx -s quit
[root@iZ23nn1p4mjZ sbin]# ps axu | grep nginx
root 27990 0.0 0.0 103256 876 pts/0 S+ 10:33 0:00 grep nginx

四、強制中止

[root@iZ23nn1p4mjZ sbin]# ./nginx -s stop

五、從新加載配置

[root@iZ23nn1p4mjZ sbin]# ./nginx -s reload

六、查看nginx版本以及編譯時候的參數

[root@iZ23nn1p4mjZ sbin]# ./nginx -V
nginx version: nginx/1.9.15
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
configure arguments: --prefix=/usr/local/nginx

-V能夠很重要,能夠看到configure的時候咱們啓用哪些模塊和參數以及編譯的gcc版本信息。

七、主要參數配置

worker_processes  1; #cpu數量

events {

    use epoll;

}

keepalive_timeout  65;

sendfile        on;

tcp_nopush     on;

tcp_nodelay     on;

http {

  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;

  server {
     listen 8081;
     server_name localhost;

注:爲何要把worker進程數量設置得與CPU核心數量一致呢?這正是Nginx與Apache服務器的不一樣之處。在Apache上每一個進程在一個時刻只處理一個請求,所以,若是但願Web服務器擁有併發處理的請求數更多,就要把Apache的進程或線程數設置得更多,一般會達到一臺服務器擁有幾百個工做進程,這樣大量的進程間切換將帶來無謂的系統資源消耗。而Nginx則否則,一個worker進程能夠同時處理的請求數只受限於內存大小,並且在架構設計上,不一樣的worker進程之間處理併發請求時幾乎沒有同步鎖的限制,orker進程一般不會進入睡眠狀態,所以,當Nginx上的進程數與CPU核心數相等時(最好每個worker進程都綁定特定的CPU核心),進程間切換的代價是最小的。

八、配置指向後端tomcat

http下增長:

upstream web-admin {
server localhost:8080 fail_timeout=15s;
server localhost:8080 fail_timeout=15s;
server localhost:8080 fail_timeout=15s;
}

location / {
root html;
index index.html index.htm;
proxy_pass http://web-admin;
}

完整:

events {
   worker_connections 1024;
   use epoll;
}


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;
tcp_nodelay     on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # upstream ebs-k3c-web { server ebs-k3c-web:8080 fail_timeout=15s; server ebs-k3c-web:8081 fail_timeout=15s; server ebs-k3c-web:8082 fail_timeout=15s; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; }

      location /ebs-k3c-web/ {
        proxy_pass http://ebs-k3c-web;
      }

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

啓動或者從新加載配置,輸入http://ip訪問。

注在看/etc/init.d/nginx腳本時,發現腳本中存在大量行爲(點號 空格  文件名),真沒見過以下:

#!/bin/sh

# Copyright (C) Igor Sysoev
# Copyright (C) Nginx, Inc.


LC_ALL=C
export LC_ALL

. auto/options . auto/init . auto/sources

test -d $NGX_OBJS || mkdir $NGX_OBJS

echo > $NGX_AUTO_HEADERS_H
echo > $NGX_AUTOCONF_ERR

查閱了資料得知:

一、 若是咱們要執行某個文件,可是此文件不可執行,此時咱們要用chmod u+x file_name來使文件具備可執行權限

二、但是有時咱們不想更改此文件的執行權限,但又想執行此文件,能夠採用(點號--空格--文件名)的形式來執行一個腳本(只有root用戶才能夠這麼作)

vs編譯nginx,參考https://github.com/topcpporg/nginx_vs

相關文章
相關標籤/搜索