Nginx的安裝(筆記)


0, 先決條件
Nginx 依賴 zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre
安裝命令:
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

pcre安裝命令:
wget http://downloads.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz;
cd pcre-8.41;
./configure
make && make install
pcre-config --version

1,Nginx 安裝
1.1, 下載 Nginx,下載地址:http://nginx.org/download/nginx-1.13.0.tar.gz
$ wget http://nginx.org/download/nginx-1.13.0.tar.gz
1.2, 解壓安裝包
$ tar -zxvf nginx-1.13.0.tar.gz
1.3, 進入安裝包目錄
$ cd nginx-1.13.0
1.4, 編譯安裝
$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-7.8
$ make
$ make install
1.5, 查看nginx版本
$ /usr/local/nginx/sbin/nginx -v
到此,nginx安裝完成。

2, Nginx 配置
2.1, 創建 Nginx 運行使用的用戶 www(可選)
$ /usr/sbin/groupadd www
$ /usr/sbin/useradd -g www www
2.2, 配置nginx.conf 默認路徑:/usr/local/nginx/conf/nginx.conf
$ cat /usr/local/nginx/conf/nginx.conf
# 訪問的用戶
# user root;
# 設置值和CPU核心數一致
worker_processes 16;

# 日志位置和日志級別
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


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;

keepalive_timeout 65;

gzip on;

upstream system_server {
server 127.0.0.1:8080;
keepalive 2000;
}

server {
listen 801;
# listen 443 ssl;
server_name 127.0.0.1;

#
# ssl on;
# ssl_certificate /usr/local/nginx/conf/ssl/server.crt;
# ssl_certificate_key /usr/local/nginx/conf/ssl/server.key;
# ssl_session_timeout 5m;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;


# charset koi8-r;

access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

# websocket
location ^~ /socket {
proxy_pass http://system_server;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

# 正則表達式匹配路徑
location ~* ^/system/.*$ {
proxy_pass http://system_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

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;
}
}

}

2.3,檢查配置文件ngnix.conf的正確性命令:
$ /usr/local/webserver/nginx/sbin/nginx -t

3, 啟動 Nginx
3.1 啟動命令如下:
$ /usr/local/webserver/nginx/sbin/nginx
3.2 訪問站點
從瀏覽器訪問我們配置的站點ip:http://127.0.0.1:9090

4, 其它
4.1 常用命令:
# 重新載入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reload
# 重啟 Nginx
/usr/local/webserver/nginx/sbin/nginx -s reopen
# 停止 Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop

4.2 location配置語法規則: location [=|^~|~|~*] /uri/ { … }
= 開頭表示普通字符開頭,匹配規則:精確匹配;
^~ 開頭表示普通字符開頭,匹配規則:uri 最長路徑匹配;
~ 開頭表示特殊字符開頭,匹配規則:正則匹配(區分大小寫);
~* 開頭表示特殊字符開頭,匹配規則:正則匹配(不區分大小寫);
!~ 匹配規則:區分大小寫不匹配的正則;
!~* 匹配規則:不區分大小寫不匹配的正則;
/ 通用匹配,任何請求都會匹配到。

4.3 rewrite 重寫規則語法: rewrite 正則 替換 標志位
4.3.1 正則表達式
^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$ ---->http://xxxx.com/images/aa/abc01/test.gif
#其中
$1=([a-z]{2}) #$1=aa
$2=([a-z0-9]{5}) #$2=abc01
$3=(.*) #$3=test
$4=(png|jpg|gif) #$4=gif
4.3.2 URI 重寫
/data?file=$3.$4 # rewrite之后的query http://data?file=test.gif
4.3.3 尾部的標記 last return break
last 標記之后會從新loaction ,繼續rewrite 最多10次;
break標記是直接跳槽rewrite和localtion 進行query的處理
return標記停止rewrite 處理指令,進而控制主HTTP 模塊處理請求,也就是HTTP請求也不處理了,直接給client 返回(結合error0page)

注意:nginx不對url做編碼。例如:請求:/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。

4.4 開機啟動

在/etc/init.d/nginx (沒有nginx文件則新建)中輸入如下命令:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
# official web       https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
#
# 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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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,需要根據安裝路徑修改下面這兩項配置:
nginx=”/usr/sbin/nginx” 修改成nginx執行程序的路徑。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路徑。

5, FAQs
5.1 NGINX啟動時提示錯誤
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解決方法:
#64位OS
ln -s /usr/local/lib/libpcre.so.1 /lib64
#32位OS
ln -s /usr/local/lib/libpcre.so.1 /lib


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM