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

相关文章
相关标签/搜索