Nginx

Nginx

Nginx是一款免费,开源,高性能的HTTP服务器和反向代理服务器。同时,也可作为IMAP/POP3代理服务器。nginx以它的高性能,稳定性,丰富的功能设置,配置简单和低资源消耗而得以著称。nginx是为数不多能够处理C10K问题的轻量级服务器。nginx不依赖于线程来处理请求。相反,它使用一个可以高扩展的事件驱动家口。这种架构使用内存小,但更重要的是,可以预测存储器在高负载下的情况。即使你不希望处理成千上万的并发请求,你仍然可以从Nginx的高性能和占用小内存特点上受益。nginx能够满足各种程度上的需求:从最小的VPS 大到服务器集群都可以满足。

在中国大陆使用nginx网站用户有:百度、京东、腾讯、网易、淘宝等。

用途:

web服务器、web反向代理服务器、smtp反向代理服务器

一、安装Nginx

1.解决依赖关系

#yum install gcc openssl-devel pcre-devel zlib-devel

2.安装

首先添加用户nginx,实现以运行nginx服务进程:

# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx

开始编译安装:

./configure   --prefix=/usr   --sbin-path=/usr/sbin/nginx   --conf-path=/etc/nginx/nginx.conf   --error-log-path=/var/log/nginx/error.log   --http-log-path=/var/log/nginx/access.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_stub_status_module   --with-http_gzip_static_module   --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/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre
#make && make install

关于配置选项的简单说明:

--prefix=<path> - The path relative to which all other Nginx paths will resolve. If not specified, defaults to /usr/local/nginx.

--sbin-path=<path> - The path to the nginx executable. Only used for installation. If not specified defaults to <prefix>/sbin/nginx.

--conf-path=<path> - The default location of nginx.conf if no -c parameter is provided. If not provided, defaults to <prefix>/conf/nginx.conf.

--pid-path=<path> - The path to nginx.pid, if not set via the "pid" directive in nginx.conf. If not provided, defaults to <prefix>/logs/nginx.pid.

--error-log-path=<path> - The location of the error log if not set via the "error_log" in nginx.conf. If not set, defaults to <prefix>/logs/error.log.

--http-log-path=<path> - The location of the access log if not set via the "access_log" directive in nginx.conf. If not set, defaults to <prefix>/logs/access.log.

--user=<user> - The default user that nginx will run as if not set in nginx.conf via the "user" directive. If not set, defaults to "nobody".

--group=<group> - The default group that nginx will run under if not set via the "user" directive in nginx.conf. If not set defaults to "nobody".

--with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.

--with-http_flv_module - Enable ngx_http_flv_module

--http-client-body-temp-path=PATH - Set path to the http client request body temporary files. If not set, defaults to <prefix>/client_body_temp

--http-proxy-temp-path=PATH - Set path to the http proxy temporary files. If not set, defaults to <prefix>/proxy_temp

--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files. If not set, defaults to <prefix>/fastcgi_temp

--lock-path=<path> - The path to the nginx.lock file. If not provided, defaults to <prefix>/logs/nginx.lock.

3.为nginx提供SysV init脚本:

新建文件/etc/rc.d/init.d/nginx,内容如下

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse #               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -`
   options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`
   for opt in $options; do
       if [ `echo $opt | grep ‘.*-temp-path‘` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

而后为才脚本赋予执行权限:


1

#chmod +x /etc/rc.d/init.d/nginx

添加至服务管理列表,并让其开机自动启动:


1

2


#chkconfig --add nginx

#chkconfig nginx on

而后就可以启动服务并测试:


1

#service nginx start

注:可以使用ab、webbench、http_load、Loadrunner、Jmeter等工具来进行网站测试。

时间: 2024-08-04 22:58:37

Nginx的相关文章

linux下Nginx配置文件(nginx.conf)配置设置详解(windows用phpstudy集成)

linux备份nginx.conf文件举例: cp /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf-20171111(日期) 在进程列表里 面找master进程,它的编号就是主进程号. ps -ef | grep nginx 查看进程 cat /usr/local/nginx/nginx.pid 每次修改完nginx文件都要重新加载配置文件linux命令: /usr/local/nginx -t //验证配置文件是否合法 若ngin

shell 格式化输出nginx的编译参数

命令 nginx -V > nginx.txt cat -n nginx.txt  | sed -n '5,18p' | awk '{$1="";print $0}'  | sed 's/^[ ]*//g'  | tr '\n' ',' | sed -n 's/,//gp' | tr " " "\n" 结果 configure arguments: --user=nginx --group=nginx --prefix=/usr/share

Nginx 反代参数:$X-Real-Ip和$X-Forwarded-For的区别

## \$X-Real-Ip和$X-Forwarded-For的区别 标签(空格分隔): nignx 负载均衡 client-ip --- ####1.如果只有一层代理,这两个头的值就是一样的####2.多层代理> * X-Forwarded-For:  header包含这样一行        `*X-Forwarded-For: 1.1.1.1, 2.2.2.2, 3.3.3.3*`> * X-Real-Ip:没有相关标准,上面的例子,如果配置了X-Read-IP,可能会有两种情况`// 最

Nginx为什么比Apache Httpd高效:原理篇

一.进程.线程? 进程是具有一定独立功能的,在计算机中已经运行的程序的实体.在早期系统中(如linux 2.4以前),进程是基本运作单位,在支持线程的系统中(如windows,linux2.6)中,线程才是基本的运作单位,而进程只是线程的容器.程序 本身只是指令.数据及其组织形式的描述,进程才是程序(那些指令和数据)的真正运行实例.若干进程有可能与同一个程序相关系,且每个进程皆可以同步(循 序)或异步(平行)的方式独立运行.现代计算机系统可在同一段时间内以进程的形式将多个程序加载到存储器中,并借

linux 安装与启动nginx

linux系统为Centos 64位 一.去http://nginx.org/download/上下载相应的版本下载nginx-1.8.0.tar.gz(注:还有更高版本的). 二.解压 tar -zxvf nginx-1.8.0.tar.gz 三.进入nginx-1.8.0/文件夹,设置一下配置信息 ./configure --prefix=/usr/local/nginx(安装后的文件存放路径). 四.配置的时候可能会出现类似这样的信息 ./configure: error: the HTT

linux运维、架构之路-Nginx提高

一.虚拟主机搭建 1.基于域名的虚拟主机 [[email protected] html]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name

linux自学笔记--nginx基本配置

1.基本配置 worker_processes auto|3; 指定使用的核数,默认auto,也可指定  一般为自身核数-1,可用lscpu查看 events { worker_connections 1024; 最大并发连接数,最大并发响应  数 worker_processes * worker_connections } http { keepalived_timeout 65 0表示禁止长连接 keepalived_request 长连接最大资源数,默认100 keepalived_di

nginx配置文件详解

nginx配置文件nginx.conf超详细讲解 #nginx进程,一般设置为和cpu核数一样worker_processes 4;                        #错误日志存放目录 error_log  /data1/logs/error.log  crit;  #运行用户,默认即是nginx,可不设置user nginx       #进程pid存放位置pid        /application/nginx/nginx.pid; #Specifies the value

Saltstack批量编译部署nginx(多模块)

最近一直在研究saltstack的同步文件和批量执行命令,随着架构的变大,批量部署的需求也变得明显起来了,我需要用一条命令就部署好nginx和tomcat,并且符合我所有的环境需求,可以直接投入生产环境使用,这就需要用到saltstack的批量安装部署功能了.这篇文章主要介绍nginx的批量部署,下篇讲解tomcat多实例的批量部署方法. 环境介绍: Centos 6.5 salt 2015.5.10 nginx 1.12.0 minion:test 1.修改master配置文件,修改后重启服务

FastDFS+Nginx问题及修复

FastDFS+nginx问题及修复:     1.[error] 30000#0: *1 open() "/usr/local/nginx/html/group1/M00/00 /00/wKgAA1cLh12AI0kfAAAADzbdjmQ50_big.html           "failed (2: No such file or directory), client: 192.168.0.181, server:localhost, request:            &