nginx依赖以下模块:

l  gzip模块需要 zlib 库

l  rewrite模块需要 pcre 库

l  ssl 功能需要openssl库

# tar xzvf nginx-1.9..tar.gz

# yum -y install pcre-devel openssl openssl-devel

# yum -y install zlib

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

  否则启动时会出错“nginx: [emerg] getpwnam(“www”) failed”

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

  注意:如果要访问环境变量,lua方式必须安装模块ngx_lua modulengx_devel_kit module (见https://github.com/openresty/lua-nginx-module#readme,https://github.com/simplresty/ngx_devel_kit,如今随着微服务的流行,nginx+lua作为网关成了必备技能),perl方式必须安装 ngx_http_perl_module module

如果以后要启用https,一定要加上--with-http_ssl_module。

make

make install

2、启动

首先,检查下端口是否被占用: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

3、停止

[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

4、强制停止

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

5、重新加载配置

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

6、查看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版本信息。

7、主要参数配置

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核心),进程间切换的代价是最小的。

8、配置指向后端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

查阅了资料得知:

1、 如果我们要执行某个文件,但是此文件不可执行,此时我们要用chmod u+x file_name来使文件具有可执行权限

2、可是有时我们不想更改此文件的执行权限,但又想执行此文件,可以采用(点号--空格--文件名)的形式来执行一个脚本(只有root用户才可以这么做)

vs编译nginx,参考https://github.com/topcpporg/nginx_vs

https的配置及强制跳转参见nginx配置https并强制http自动跳转到https

centos下nginx安装与配置的更多相关文章

  1. Centos下 Nginx安装与配置

    网上找了好多资料.都很难找全,这里以这个目录为主,进行备注. Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供 ...

  2. centos下Nginx安装和配置多个域名的虚拟主机

    nginx安装步骤,源码编译安装(源码编译,可以自定制更多功能) openssl #user nobody; worker_processes ; #error_log logs/error.log; ...

  3. centos下nginx安装和配置

    注:此文是根据前辈的博客和自己实际动手总结出来的,不喜勿喷 1.准备工作 Nginx的安装依赖于以下三个包,意思就是在安装Nginx之前首先必须安装一下的三个包,注意安装顺序如下: 1 SSL功能需要 ...

  4. CentOS 下 redis 安装与配置

    CentOS 下 redis 安装与配置   1.到官网上找到合适版本下载解压安装 [root@java src]# wget -c http://redis.googlecode.com/files ...

  5. centos7系统下nginx安装并配置开机自启动操作

    准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcr ...

  6. nginx在CentOs下的安装及配置

    前言: 先介绍一下nginx: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.其特点是占有内存少,并发能力强, ...

  7. windows下nginx安装、配置与使用(转载)

    目前国内各大门户网站已经部署了Nginx,如新浪.网易.腾讯等:国内几个重要的视频分享网站也部署了Nginx,如六房间.酷6等.新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx ...

  8. windows下nginx安装、配置与使用

    目前国内各大门户网站已经部署了Nginx,如新浪.网易.腾讯等:国内几个重要的视频分享网站也部署了Nginx,如六房间.酷6等.新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx ...

  9. CentOS下nginx+php的配置及nginx开机启动配置

    关闭防火墙 (不然外链接是访问不了 apache) service iptables stop 关闭安全系统 SELinux( 不然报403 访问页面错误 ) 1.Nginx安装主要在于配置文件的修改 ...

随机推荐

  1. The History of Operating Systems

    COMPPUTER SCIENCE AN OVERVIEW 11th Edition job 作业 batch processing 批处理 queue 队列 job queue 作业队列 first ...

  2. Windows Preinstallation Environment

    https://en.wikipedia.org/wiki/Windows_Preinstallation_Environment https://zh.wikipedia.org/wiki/Wind ...

  3. df and du

    1.若有进程在占用某个文件,而其他进程把这文件删掉,只会删除其在磁盘中的标记,而不会释放其占用的磁盘空间:直到所有访问该文件的进程退出为止: 2.df 是从内核中获取磁盘占用情况数据的,而du是统计当 ...

  4. sql中exist()的用法

    转自:https://www.cnblogs.com/netserver/archive/2008/12/25/1362615.html 比如在Northwind数据库中有一个查询为 SELECT c ...

  5. 【Pyton】【小甲鱼】爬虫4-XXOO

    import urllib.request import os def open_url(url): req=urllib.request.Request(url) req.add_header('U ...

  6. SQLServer DBA 三十问

    原贴:http://www.cnblogs.com/fygh/archive/2011/10/18/2216166.html 答案:https://blog.csdn.net/cjssimei527/ ...

  7. python 面向对象 析构方法

    实例化但从来没有调用他,就浪费了,就应该自动删除它 这个实例一直存在内存里 python有个垃圾自动回收机制 , 每段时间会自动刷新整个内存,把内存垃圾东西删除   析构函数: 在实例释放.销毁的时候 ...

  8. dedecms首页调用随机文章全自动时时更新

    dedecms织梦系统是全站生成静态html的,这个对搜索引擎比较友好,但是有时我们要调用文章,让蜘蛛每次来访问都感觉像是有添加新内容一样,要如何做到呢? 可以添加以下dedecms随机文章调用的参数 ...

  9. 分享一个自定义的 console 类,让你不再纠结JS中的调试代码的兼容

    问题的产生 在写JS的过程中,为了调试我们常常会写很多 console.log.console.info.console.group.console.warn.console.error代码来查看JS ...

  10. C# winform webbrowser如何指定内核为IE11? 输出 this.webbrowser.Version 显示版本是IE11的,但实际版本不是啊! 网上打的修改注册表HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULA

    最佳答案   1)假设你应用程序的名字为MyApplication.exe 2)运行Regedit,打开注册表,找到 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\M ...