linux 安装nginx

https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/

 http://nginx.org/en/download.html

1、 cd /usr/local/src

wget https://www.openssl.org/source/openssl-1.1.1b.tar.gz

tar xzvf openssl-1.1.1b.tar.gz

cd openssl-1.1.1b

./config

make

make install

安装pcre的库 , yum  install -y pcre-devel

安装zlib , yum install -y zlib-devel

2、 wget http://59.80.44.46/nginx.org/download/nginx-1.14.2.tar.gz

3、tar xzvf nginx-1.14.2

4、cd nginx-1.14.2

5、./configure --prefix=/usr/local/nginx

./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module  --with-pcre  --with-http_ssl_module

9、make && make install

10、 cd /usr/local/nginx

11、启动 /usr/local/nginx/sbin/nginx

如果报错 :./nginx: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

需要

在root用户下执行:

  1. ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
  2. ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

12、查看  netstat -antp

13、如果没有netstat , 安装, yum install -y net-tools.x86_64

-------------------------------------------------------------

配置


以上安装方法nginx的配置文件位于

/usr/local/nginx/conf/nginx.conf

Nginx配置文件常见结构的从外到内依次是「http」「server」「location」等等,缺省的继承关系是从外到内,也就是说内层块会自动获取外层块的值作为缺省值。

Server

接收请求的服务器需要将不同的请求按规则转发到不同的后端服务器上,在 nginx 中我们可以通过构建虚拟主机(server)的概念来将这些不同的服务配置隔离。

server {
    listen       80;
    server_name  localhost;
    root   html;
    index  index.html index.htm;
}

例如我们笔戈玩下的两个子项目 passport 和 wan 就可以通过在 nginx 的配置文件中配置两个 server,servername 分别为 passport.bigertech.com 和 wan.bigertech.com。这样的话不同的 url 请求就会对应到 nginx 相应的设置,转发到不同的后端服务器上。

这里的 listen 指监听端口,server_name 用来指定IP或域名,多个域名对应统一规则可以空格分开,index 用于设定访问的默认首页地址,root 指令用于指定虚拟主机的网页跟目录,这个地方可以是相对地址也可以是绝对地址。

通常情况下我们可以在 nginx.conf 中配置多个server,对不同的请求进行设置。就像这样:

server {
    listen       80;
    server_name  host1;
    root   html;
    index  index.html index.htm;
}
server {
    listen       80;
    server_name  host2;
    root   /data/www/html;
    index  index.html index.htm;
}

但是当 server 超过2个时,建议将不同对虚拟主机的配置放在另一个文件中,然后通过在主配置文件 nginx.conf 加上 include 指令包含进来。更便于管理。

 在conf 下建立 vhosts子目录, 在vhosts子目录下建立每个server的配置文件

如:ServerManager.conf

vim ServerManager.conf

server {
        listen       8000;
        server_name  server.smartyt.net;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_pass http://127.0.0.1:5001;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

然后在 nginx.conf 文件的http段最后加上:

(nginx.conf和vhosts在同一路径下)

include vhosts/*.conf;

就可以把vhosts的文件都包含进去啦。

Localtion

每个 url 请求都会对应的一个服务,nginx 进行处理转发或者是本地的一个文件路径,或者是其他服务器的一个服务路径。而这个路径的匹配是通过 location 来进行的。我们可以将 server 当做对应一个域名进行的配置,而 location 是在一个域名下对更精细的路径进行配置。

以上面的例子,可以将root和index指令放到一个location中,那么只有在匹配到这个location时才会访问root后的内容:

    location / {
        root   /data/www/host2;
        index  index.html index.htm;
    }

location 匹配规则

~      波浪线表示执行一个正则匹配,区分大小写
~*    表示执行一个正则匹配,不区分大小写
^~    ^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
=      进行普通字符精确匹配

匹配例子:

location  = / {
  # 只匹配"/".
  [ configuration A ] 
}
location  / {
  # 匹配任何请求,因为所有请求都是以"/"开始
  # 但是更长字符匹配或者正则表达式匹配会优先匹配
  [ configuration B ] 
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
  [ configuration C ] 
}
location ~* .(gif|jpg|jpeg)$ {
  # 匹配以 gif, jpg, or jpeg结尾的请求. 
  # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.   
  [ configuration D ] 
}

请求:
/ -> 符合configuration A
/documents/document.html -> 符合configuration B
/images/1.gif -> 符合configuration C
/documents/1.jpg ->符合 configuration D

静态文件映射

访问文件的配置主要有 root 和 aliasp's 两个指令。这两个指令的区别容易弄混:

alias

alias后跟的指定目录是准确的,并且末尾必须加 /。

    location /c/ {
        alias /a/;
    }

如果访问站点http://location/c访问的就是/a/目录下的站点信息。

root

root后跟的指定目录是上级目录,并且该上级目录下要含有和location后指定名称的同名目录才行。

    location /c/ {
        root /a/;
    }

这时访问站点http://location/c访问的就是/a/c目录下的站点信息。

如果你需要将这个目录展开,在这个location的末尾加上「autoindex on; 」就可以了

转发

配置起来很简单比如我要将所有的请求到转移到真正提供服务的一台机器的 8001 端口,只要这样:

location / {
    proxy_pass 172.16.1.1:8001;
}

这样访问host时,就都被转发到 172.16.1.1的8001端口去了。

负载均衡

upstream myserver; {
    ip_hash;    
    server 172.16.1.1:8001;
    server 172.16.1.2:8002;
    server 172.16.1.3;
    server 172.16.1.4;
}
location / {
    proxy_pass http://myserver;
}

我们在 upstream 中指定了一组机器,并将这个组命名为 myserver,这样在 proxypass 中只要将请求转移到 myserver 这个 upstream 中我们就实现了在四台机器的反向代理加负载均衡。其中的 ip_hash 指明了我们均衡的方式是按照用户的 ip 地址进行分配。另外还有轮询、指定权重轮询、fair、url_hash几种调度算法。

总结


以上是最简单的通过 nginx 实现静态文件转发、反向代理和负载均衡的配置。在 nginx 中所有的功能都是通过模块来实现的,比如当我们配置 upstream 时是用 upstream 模块,而 server 和 location 是在 http core 模块,其他的还有流控的 limt 模块,邮件的 mail 模块,https 的 ssl 模块。他们的配置都是类似的可以再 nginx 的模块文档中找到详细的配置说明。

----------------------------------------------------------------------------------------------------------------------------------------------------

Linux(CentOS)下设置nginx开机自动启动

首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:

1
vim /etc/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' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   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
复制代码

这个脚本来自nginx官方,脚本地址:http://wiki.nginx.org/RedHatNginxInitScript ,不过要注意,如果你是自定义编译安装的nginx,需要根据您的安装路径修改下面这两项配置:

nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。

NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。

保存脚本文件后设置文件的执行权限:

chmod a+x /etc/init.d/nginx
然后,就可以通过该脚本对nginx服务进行管理了:
/etc/init.d/nginx start
/etc/init.d/nginx stop

使用chkconfig进行管理

上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便,比如要设置nginx开机启动等。这时可以使用chkconfig来设置。

先将nginx服务加入chkconfig管理列表:

chkconfig --add /etc/init.d/nginx
加完这个之后,就可以使用service对nginx进行启动,重启等操作了。
service nginx start
service nginx stop

设置终端模式开机启动:

chkconfig nginx on

参考自:http://blog.csdn.net/boyish_/article/details/51768784

---------------------------------------------------------------------------------------

TERM, INT 立刻杀掉

QUIT  优雅的关闭进程,等待请求结束后再关闭

HUP 改变配置文件,,平滑的重读配置文件

USR1 重读日志,在日志按月/日分割时有用

USR2  平滑升级

WINCH 优雅关闭旧进程, 配合USER2进行升级

语法

Kill -信号选项 nginx 主进程号

Kill -HUP 873

kill -信号控制 `cat /xxx/path/log/nginx.pid`

kill; -USER1 `cat /xxx/path/log/nginx.pid`

ps aux | grep nginx

kill -INT pid  

或者

/usr/local/nginx/sbin/nginx -s reload  平滑重启

/usr/local/nginx/sbin/nginx -s stop 平滑停止

/usr/local/nginx/sbin/nginx -t 测试配置文件

重新编译

make clean

./configure --prefix=/usr/local/nginx --add-module=/app/ngx_http_consistent_hash-master -with-http_stub_status_module

安装统计模块,便于观察nginx -with-http_stub_status_module

vim /usr/local/nginx.conf

location /status{
  stub_status on;

  access_log off;

  allow 192.168.0.1;  #只允许自己的ip

  deny all;

}

用 ab压力测试

优化nginx响应请求:

1建立socket连接 2打开文件,并沿socket返回

排查问题 主要从系统的dmesg 和nginx的error.log来观察

ulimit -n  #查看系统允许打开的文件数

ulimit -n 10000 改为 10000

nginx 子进程允许打开的连接 worker_connections 10240

nginx 一个工作进程允许打开的文件数 worker_rlimit_nofile 10000

worker_rlimit_nofile 10000

events {

worker_connections 10240

}

连接保持时间改为0,不保持(或保持在2秒以内)

keepalive_timeout 0;

系统层面 最大连接数

echo 50000>/proc/sys/net/core/somaxconn

系统层面 加快tcp连接的回收 -> 加快

echo 1>/proc/sys/net/ipv4/tcp_tw_recycle

系统层面 是否允许回收利用 -> 回收利用

echo 1>/proc/sys/net/ipv4/tcp_tw_reuse

系统层面 是否做洪水抵御 -> 不做

echo 0>/proc/sys/net/ipv4/tcp_syncookies

ulimit -n 设置一个大的值

原文地址:https://www.cnblogs.com/leon-ytparty/p/10550237.html