下载文件

http://nginx.org/en/download.html 下载 nginx-1.9.3.tar.gz

安装Nginx

安装
  1. 一、安装nginx时必须先安装相应的编译工具
  2. yum -y install gcc gcc-c++yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
  3.  
  4. 建立nginx
  5. groupadd -r nginx
    # -r 表示创建的是系统组
  6. useradd -s /sbin/nologin -g nginx -r nginx
    # -r 表示创建的是系统用户
  7. id nginx
    # 即使用其他用户启动nginx, 也必须创建nginx用户和用户组, 否则会出现 nginx: [emerg] getpwnam("nginx") failed 错误
  8.  
  9. zlib:nginx提供gzip模块,需要zlib库支持
  10. openssl:nginx提供ssl功能
  11. pcre:支持地址重写rewrite功能
  12.  
  13. 二、tar -zxvf nginx-1.9.3.tar.gz
  14.  
  15. 三、cd nginx-1.9.
  16.  
  17. 四、./configure \
  18. --prefix=/usr \
  19. --sbin-path=/usr/sbin/nginx \
  20. --conf-path=/etc/nginx/nginx.conf \
  21. --error-log-path=/var/log/nginx/error.log \
  22. --pid-path=/var/run/nginx/nginx.pid \
  23. --user=nginx \
  24. --group=nginx \
  25. --with-http_ssl_module \
  26. --with-http_flv_module \
  27. --with-http_gzip_static_module \
  28. --http-log-path=/var/log/nginx/access.log \
  29. --http-client-body-temp-path=/var/tmp/nginx/client \
  30. --http-proxy-temp-path=/var/tmp/nginx/proxy \
  31. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
  32. --with-http_stub_status_module
  33.  
  34. 我用的参数是
    ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-pcre --with-http_ssl_module --with-stream --with-stream_ssl_module
  35.  
  36. 五、make && make install
如果openssl是编译安装的, 使用以下命令
  1. ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-pcre --with-http_ssl_module --with-openssl=/usr/src/openssl-1.0.1p/ --with-http_stub_status_module --with-stream --with-stream_ssl_module
  2.  
  3. # stub_status模块主要用于查看Nginx的一些状态信息
  4. # with-openssl 指定 openssl 的源码目录
 
启动关闭
  1. #启动nginx
  2.  
  3. sudo /opt/nginx/sbin/nginx
  4.  
  5. #查看nginx进程
  6. ps aux|grep nginx
  7.  
  8. nginx -s reload :修改配置后重新加载生效
  9. nginx -s reopen :重新打开日志文件
  10.  
  11. nginx -c /path/to/nginx.conf 指定配置文件启动nginx
  12. nginx -t -c /path/to/nginx.conf 测试nginx配置文件, 但不启动
  13.  
  14. #关闭nginx:
  15. nginx -s stop :快速停止
  16. nginx -s quit :完整有序的停止

其他的停止nginx 方式:

  1. ps -ef | grep nginx
  2. kill -QUIT 主进程号 :从容停止Nginx
  3. kill -TERM 主进程号 :快速停止Nginx
  4. pkill - nginx :强制停止Nginx

参考资料

http://ilz.me/2015/04/29/nginx-190-make/ Nginx1.9.0编译安装过程, 带geoip的编译
http://www.cnblogs.com/zhuhongbao/archive/2013/06/04/3118061.html nginx1.2.8版本的安装及配置

使用非root用户启动/关闭Nginx

首先把nginx的owner设为tomcat

  1. sudo chown -R tomcat:tomcat /opt/nginx

更精确一点, 需要设置owner为tomcat的目录包括: fastcgi_temp, log 和 proxy_temp, 目录的权限详细为:

  1. [root@bogon nginx]# ll
  2. total
  3. drwx------ nginx root Dec : client_body_temp
  4. drwxr-xr-x root root Jan : conf
  5. drwx------ tomcat tomcat Dec : fastcgi_temp
  6. drwxr-xr-x root root Dec : html
  7. drwxr-xr-x tomcat tomcat Jan : logs
  8. drwx------ tomcat tomcat Jan : proxy_temp
  9. drwxr-xr-x root root Dec : sbin
  10. drwx------ nginx root Dec : scgi_temp
  11. drwx------ nginx root Dec : uwsgi_temp

使用非root用户启动nginx出现端口绑定权限错误的处理
错误: nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
参考 https://wiki.apache.org/httpd/NonRootPortBinding

1. 通过setcap
这个方法需要较高的内核版本: Requires a not-ancient linux kernel (2.6.24 or later), Centos6及以上可以

  1. # sudo setcap cap_net_bind_service=+ep /opt/nginx/sbin/nginx

检查是否capability is added:

  1. # getcap /opt/nginx/sbin/nginx
  2. /opt/nginx/sbin/nginx = cap_net_bind_service+ep

2. 较通用的办法, 通过iptables, nat based method to redirect traffic from port 80 to 8080.
例如

  1. # iptables -t nat -A PREROUTING -d <ip> -p tcp --dport -m addrtype --dst-type LOCAL -j DNAT --to-destination <ip>:
  2. # iptables -t nat -A OUTPUT -d <ip> -p tcp --dport -m addrtype --dst-type LOCAL -j DNAT --to-destination <ip>:
  3. or
  4. # iptables -t nat -A PREROUTING -i eth0 -p tcp --dport http -j REDIRECT --to-ports
  5. # iptables -t nat -A PREROUTING -i eth0 -p tcp --dport -j REDIRECT --to-port
  6. # iptables-save
  7. # this redirects incoming connections on port to port

附: iptable参数说明: http://ipset.netfilter.org/iptables.man.html https://help.ubuntu.com/community/IptablesHowTo

Nginx配置

  1. #user tomcat;
  2. worker_processes ; #启动进程,通常设置成和cpu的数量相等
  3.  
  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7.  
  8. pid logs/nginx.pid;
  9.  
  10. events {
  11. use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式, 仅用于linux2.6以上内核, 可提高nginx性能
  12. worker_connections ;
  13. }
  14.  
  15. http {
  16. include mime.types; #设定mime类型,类型由mime.type文件定义
  17. default_type application/octet-stream;
  18.  
  19. # 日志格式, 如果access_log 或者是虚拟主机里的access_log启用了, 这个也要启用, 否则启动时会有警告
  20. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  21. '$status $body_bytes_sent "$http_referer" '
  22. '"$http_user_agent" "$http_x_forwarded_for"';
  23.  
  24. #access_log logs/access.log main;
  25.  
  26. sendfile on; #指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件. 对于普通应用必须设为 on, 如果用来进行下载等应用磁盘IO重负载应用可设置为 off, 以平衡磁盘与网络I/O处理速度, 降低系统的uptime.
  27. #tcp_nopush on;
  28.  
  29. #keepalive_timeout ;
  30. keepalive_timeout ; #连接超时时间
  31.  
  32. gzip on; #开启gzip压缩
  33.  
  34. server {
  35. listen ;
  36. server_name localhost;
  37. #charset koi8-r;
  38. #access_log logs/host.access.log main;
  39.  
  40. #默认请求
  41. location / {
  42. root html; #定义服务器的默认网站根目录位置
  43. index index.html index.htm;
  44. }
  45.  
  46. #error_page /.html;
  47. # redirect server error pages to the static page /50x.html
  48. error_page /50x.html;
  49. location = /50x.html {
  50. root html;
  51. }
  52.  
  53. # proxy the PHP scripts to Apache listening on 127.0.0.1:
  54. #
  55. #location ~ \.php$ {
  56. # proxy_pass http://127.0.0.1;
  57. #}
  58.  
  59. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
  60. #
  61. #location ~ \.php$ {
  62. # root html;
  63. # fastcgi_pass 127.0.0.1:;
  64. # fastcgi_index index.php;
  65. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  66. # include fastcgi_params;
  67. #}
  68.  
  69. # deny access to .htaccess files, if Apache's document root
  70. # concurs with nginx's one
  71. #
  72. #location ~ /\.ht {
  73. # deny all;
  74. #}
  75. }
  76.  
  77. # another virtual host using mix of IP-, name-, and port-based configuration
  78. #
  79. #server {
  80. # listen ;
  81. # listen somename:;
  82. # server_name somename alias another.alias;
  83.  
  84. # location / {
  85. # root html;
  86. # index index.html index.htm;
  87. # }
  88. #}
  89.  
  90. # 增加同端口不同域名的虚拟主机
  91. # 可以放到子目录下再include进来, 如 include vhost/cc.com.conf;
  92. server {
  93. listen ;
  94. server_name demo.rb.com;
  95. location / {
  96. root /var/www/html;
  97. index index.html index.htm index.php;
  98. }
  99. location /images/ {
  100. # 使用root时, 服务器会去找 /opt/nginx/html/images 目录
  101. root /opt/nginx/html;
  102. }
  103. location /images2/ {
  104. # 使用alias时, 服务器找的才是/opt/nginx/html目录
  105. # 这是一个严格的匹配, 所以如果location 以/结束, 下面的alias也要以/结束
  106. alias /opt/nginx/html/;
  107. }
  108. access_log logs/demo.rb.com.access.log main;
  109. }
  110.  
  111. # HTTPS server
  112. #
  113. #server {
  114. # listen ssl;
  115. # server_name localhost;
  116.  
  117. # ssl_certificate cert.pem;
  118. # ssl_certificate_key cert.key;
  119.  
  120. # ssl_session_cache shared:SSL:1m;
  121. # ssl_session_timeout 5m;
  122.  
  123. # ssl_ciphers HIGH:!aNULL:!MD5;
  124. # ssl_prefer_server_ciphers on;
  125.  
  126. # location / {
  127. # root html;
  128. # index index.html index.htm;
  129. # }
  130. #}
  131.  
  132. }

开启 stub status

  1. nginx.confserver块中添加如下代码
  2.  
  3. location /nginx_status {
  4. # Turn on nginx stats
  5. stub_status on;
  6. # I do not need logs for stats
  7. access_log off;
  8. # Security: Only allow access from 192.168.1.100 IP #
  9. #allow 192.168.1.100;
  10. # Send rest of the world to /dev/null #
  11. #deny all;
  12. }
  13.  
  14. 这段代码是加在默认的server里的,
  15. 假设默认server的配置为
  16.  
  17. listen 127.0.0.1:;
  18. server_name 127.0.0.1;
  19.  
  20. 那么访问nginx的状态,就可以通过 curl 127.0.0.1/nginx_status访问了

自定义启动脚本

  1. if [ $(ps -ef |grep "nginx" |grep -v "grep" |wc -l) -gt ];then
  2. echo "Trying to quit existing nginx processes..."
  3. if $(/opt/nginx/sbin/nginx -s quit);then
  4. echo "Nginx quited."
  5. else
  6. echo "Failed to quietly quit Nginx."
  7. if $(/opt/nginx/sbin/nginx -s stop);then
  8. echo "Nginx stopped."
  9. else
  10. echo "Failed to stop Nginx, please kill the process."
  11. exit
  12. fi
  13. fi
  14. else
  15. echo "No existing Nginx processes."
  16. fi
  17.  
  18. echo "Starting the nginx service..."
  19. if $(/opt/nginx/sbin/nginx);then
  20. echo "Nginx started."
  21. else
  22. echo "Failed to start Nginx."
  23. fi

一个用于添加到init.d服务的nginx服务脚本(未测试)

  1. #!/bin/bash
  2. # nginx Startup script for the Nginx HTTP Server
  3. # this script create it by ivan at 2010.12..
  4. #
  5. # chkconfig: -
  6. # description: Nginx is a high-performance web and proxy server.
  7. # It has a lot of features, but it's not for everyone.
  8. # processname: nginx
  9. # pidfile: /var/run/nginx.pid
  10. # config: /etc/nginx.conf
  11.  
  12. nginxd=/usr/local/nginx/sbin/nginx
  13. nginx_config=/usr/local/nginx/conf/nginx.conf
  14. nginx_pid=/usr/local/nginx/run/nginx.pid
  15.  
  16. RETVAL=
  17. prog="nginx"
  18.  
  19. # Source function library.
  20. . /etc/rc.d/init.d/functions
  21.  
  22. # Source networking configuration.
  23. . /etc/sysconfig/network
  24.  
  25. # Check that networking is up.
  26. [ ${NETWORKING} = "no" ] && exit
  27. [ -x $nginxd ] || exit
  28.  
  29. # Start nginx daemons functions.
  30. start(){
  31.  
  32. if [ -e $nginx_pid ]; then
  33. echo "nginx already running..."
  34. exit
  35. fi
  36. echo -n $"Starting $prog:"
  37. daemon $nginxd -c ${nginx_config}
  38. RETVAL=$?
  39. echo
  40. [ $RETVAL = ] && touch /var/lock/subsys/nginx
  41. return $RETVAL
  42. }
  43.  
  44. # Stop nginx daemons functions.
  45. stop(){
  46. echo -n $"Stopping $prog:"
  47. killproc $nginxd
  48. RETVAL=$?
  49. echo
  50. [ $RETVAL = ] && rm -f /var/lock/subsys/nginx $nginx_pid
  51. }
  52.  
  53. #reload nginx service functions.
  54. reload(){
  55. echo -n $"Reloading $proc:"
  56. killproc $nginxd -HUP
  57. RETVAL=$?
  58. echo
  59. }
  60. # See how we were called.
  61. case "$1" in
  62. start)
  63. start
  64. ;;
  65. stop)
  66. stop
  67. ;;
  68. reload)
  69. reload
  70. ;;
  71. restart)
  72. stop
  73. start
  74. ;;
  75. status)
  76. status $prog
  77. RETVAL=$?
  78. ;;
  79. *)
  80. echo $"Usage: $prog {start|stop|restart|reload|status|help}"
  81. exit
  82. esac
  83.  
  84. exit $RETVAL

让日志文件名按日期生成

  1. if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
  2. set $year $;
  3. set $month $;
  4. set $day $;
  5. }
  6.  
  7. access_log /var/log/nginx/$year-$month-$day-access.log;

让日志记录cookie

  1. set $dm_cookie "";
  2. if ($http_cookie ~* "(.+)(?:;|$)") {
  3. set $dm_cookie $;
  4. }
  5.  
  6. # 然后在日志格式中添加 $dm_cookie

Nginx1.9.0的安装的更多相关文章

  1. Centos 7 nginx-1.12.0编译安装

    参考:http://www.nginx.cn/install 也不知道我的系统是否有这些依赖包,试试吧?缺少哪些我就装哪些吧,多踏点坑总是能学到点东西的.   获取nginx包 http://ngin ...

  2. CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14

    准备篇: CentOS 7.0系统安装配置图解教程 http://www.osyunwei.com/archives/7829.html 一.配置防火墙,开启80端口.3306端口 CentOS 7. ...

  3. CentOS7 编译安装 nginx-1.10.0

    对于NGINX 支持epoll模型 epoll模型的优点 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的 ...

  4. CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.13

    CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.132013-10-24 15:31:12标签:服务器 防火墙 file 配置文件 written 一.配置好I ...

  5. CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.13+博客系统WordPress3.3.2

    说明: 操作系统:CentOS 6.2 32位 系统安装教程:CentOS 6.2安装(超级详细图解教程): http://www.osyunwei.com/archives/1537.html 准备 ...

  6. 编译安装LNMP Centos 6.5 x64 + Nginx1.6.0 + PHP5.5.13 + Mysql5.6.19

    (来自:http://www.cnblogs.com/vicowong/archive/2011/12/01/2116212.html) 环境: 系统硬件:vmware vsphere (CPU:2* ...

  7. CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14方法分享

    一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...

  8. Linux(CentOS6.5)下编译安装Nginx官方最新稳定版(nginx-1.10.0)

    注:此文已经更新为新版:http://comexchan.cnblogs.com/p/5815753.html ,请直接查看新版,谢谢! 本文地址http://comexchan.cnblogs.co ...

  9. nginx1.14.0下载、安装、启动

    nginx1.14.0下载及安装 wget http://nginx.org/download/nginx-1.14.0.tar.gztar -zxvf nginx-1.14.0.tar.gzcd n ...

随机推荐

  1. iOS 简单动画 block动画

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UIView * view = [ ...

  2. Android中应用程序清除data/data,清除cache,超详细

    清除data,清除cache,其实在Android原生Setting里面有这个功能的. 需求是把这个功能做到自己的App里面,并计算出cache和data的size. 所以参考了一下Setting的源 ...

  3. python 获取星期几

    In [17]: now.strftime(%a),now.strftime(%w) Out[17]: ('Mon', '1') Directive Meaning %a Weekday name. ...

  4. 持续集成(CI)初探

    前不久接触了持续集成(Continuous Integration,CI). 一.持续集成是什么 首先说说“集成”的概念.在实际的软件开发中,常常会发生两种情境: 1.几个项目组对同一个系统的不同功能 ...

  5. android ProGuard 代码混淆实现

    1 修改project.properties,添加ProGuard配置项 proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt: ...

  6. JavaScript(一)——简介(简单介绍)

    1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司(已被Oracle收购了),J ...

  7. C#解决验证码问题

    string ss = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";            R ...

  8. Linux[Fedora]查找文件包含的字段

    find 与 grep组合查找 find . –name '文件类型' | xargs grep –n '查找内容'文件类型可正则表达式通配, [.]表示当前目录下进行查找,也可自由指定目录.比如: ...

  9. Servlet/JSP-05 Cookie

    一. 问题? HTTP协议是一种无状态协议,服务器本身无法识别出哪些请求是同一个浏览器发出的,浏览器的每一次请求都是独立的.现实业务中服务器有时候需要识别来自同一个浏览器的一系列请求,例如购物车,登录 ...

  10. 查看Android支持的硬解码信息

    通过/system/etc/media_codecs.xml可以确定当前设备支持哪些硬解码.通过/system/etc/media_profiles.xml可以知道设备支持的具体profile和lev ...