日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

Nginx的安装(笔记)

發布時間:2023/11/29 Nginx 92 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 0nginx="/usr/sbin/nginx" prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs() {# make required directoriesuser=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`if [ -n "$user" ]; thenif [ -z "`grep $user /etc/passwd`" ]; thenuseradd -M -s /bin/nologin $userfioptions=`$nginx -V 2>&1 | grep 'configure arguments:'`for opt in $options; doif [ `echo $opt | grep '.*-temp-path'` ]; thenvalue=`echo $opt | cut -d "=" -f 2`if [ ! -d "$value" ]; then# echo "creating" $valuemkdir -p $value && chown -R $user $valuefifidonefi }start() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6make_dirsecho -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval }stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval }restart() {configtest || return $?stopsleep 1start }reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $nginx -HUPRETVAL=$?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" instart)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

轉載于:https://www.cnblogs.com/cityspace/p/6806138.html

總結

以上是生活随笔為你收集整理的Nginx的安装(笔记)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。