第一部分----nginx基本應用
源碼編譯安裝nginx
1、安裝pcre軟件包(使nginx支持http rewrite模塊)
yum?install?-y?pcre yum?install?-y?pcre-devel
2、安裝openssl-devel(使nginx支持ssl)
yum?install?-y?openssl-devel
3、創建用戶nginx
useradd?nginx passwd?nginx
4、安裝nginx
[root@localhost?~]tar?-vzxf?nginx-1.11.3.tar.gz?-C?/usr/local [root@localhost?~]cd?nginx-1.11.3/ [root@localhost?nginx-1.11.3]#?./configure?\ >?--group=nginx?\ >?--user=nginx?\ >?--prefix=/usr/local/nginx?\ >?--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?\ >?--http-client-body-temp-path=/tmp/nginx/client_body?\ >?--http-proxy-temp-path=/tmp/nginx/proxy?\ >?--http-fastcgi-temp-path=/tmp/nginx/fastcgi?\ >?--pid-path=/var/run/nginx.pid?\ >?--lock-path=/var/lock/nginx?\ >?--with-http_stub_status_module?\ >?--with-http_ssl_module?\ >?--with-http_gzip_static_module?\ >?--with-pcre [root@localhost?nginx-1.11.3]#?make?&&make?install
5、修改配置文件/etc/nginx/nginx.conf
#全局參數設置 worker_processes??1;?????#設置nginx啟動進程的數量,一般設置成與邏輯cpu數量相同 error_log??logs/error.log;????#指定錯誤日志 worker_rlimit_nofile?102400;????#設置一個nginx進程能打開的最大文件數 pid????????/var/run/nginx.pid;events?{worker_connections??1024;???#設置一個進程的最大并發連接數 }#http服務相關設置 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??/var/log/nginx/access.log??main;????#設置訪問日志的位置和格式sendfile????????on;?#是否調用sendfile函數輸出文件,一般設置為on,若nginx是用來進行磁盤IO負載應用時,可以設置為off,降低系統負載gzip????????????on;?#是否開啟gzip壓縮keepalive_timeout??65;??????#設置長連接的超時時間 #虛擬服務器的相關設置server?{listen???????80;????????#設置監聽的端口server_name??localhost;?????????#設置綁定的主機名、域名或ip地址charset?koi8-r;????#設置編碼字符location?/?{root???/var/www/nginx;??????#設置服務器默認網站的根目錄位置index??index.html?index.htm;????????#設置默認打開的文檔}error_page???500?502?503?504??/50x.html;????????#設置錯誤信息返回頁面location?=?/50x.html?{root???html;????????#這里的絕對位置是/var/www/nginx/html}}}
6、檢測nginx配置文件是否正確
nginx?-t
7、啟動nginx服務
nginx
8、通過nginx -s控制nginx服務
nginx?-s?stop?????#停止服務 nginx?-s?quit?????#退出服務 nginx?-s?reopen????#重新打開日志文件 nginx?-s?reload????#重新加載配置文件
9、實現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?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:"?|?sed?'s/[^*]*--user=\([^?]*\).*/\1/g'?-`options=`$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?$valuefifidone }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--------------------------------------------------------------------------
? ? 2、添加權限
chmod?+x?/etc/init.d/nginx
????3、設置開機自啟
chkconfig?nginx?on
10、nginx日志文件詳解
????nginx日志文件分為log_format和access_log兩部分
????log_format定義記錄的格式,其語法格式為
????????log_format????????樣式名稱????????樣式詳情
????配置文件中默認有
?log_format??main??'$remote_addr?-?$remote_user?[$time_local]?"$request"?''$status?$body_bytes_sent?"$http_referer"?''"$http_user_agent"?"$http_x_forwarded_for"';
變量 | 說明 |
$remote_addr和$http_x_forwarded_for | 客戶端的ip |
$remote_user | 客戶端的名稱 |
$time_local | 訪問時的本地時間 |
$request | 請求的URL和http協議 |
$status | 訪問的狀態碼 |
$body_bytes_sent | 發送給客戶端的主體內容大小 |
$http_referer | 記錄客戶端是從哪個頁面鏈接訪問過來的,若沒有鏈接,則訪問‘-’ |
$http_user_agent | 記錄客戶端使用的瀏覽器的相關信息 |
????access_log主要指定使用哪種格式記錄和日志文件的位置,其語法格式為
access_log????日志文件路徑????????樣式名稱
????如:
access_log????/var/log/nginx/access.log????main;
下面是日志內容的截圖示例
第二部分-----nginx高級應用
????1、使用alias實現虛擬目錄
location?/lzs?{alias?/var/www/lzs;index?index.html;????????#訪問http://x.x.x.x/lzs時實際上訪問的是/var/www/lzs/index/html
????2、通過stub_status模塊監控nginx的工作狀態
????????1、通過nginx -V命令查看是否已安裝stnb_status模塊
(可以發現已經安裝了~~~)
????????
????????2、編輯/etc/nginx/nginx.conf配置文件
#添加以下內容~~ location?/nginx-status?{stub_status?on;access_log????/var/log/nginx/nginxstatus.log;????#設置日志文件的位置auth_basic????"nginx-status";????#指定認證機制(與location后面的內容相同即可)auth_basic_user_file????/etc/nginx/htpasswd;????????#指定認證的密碼文件}
????????3、創建認證口令文件并添加用戶lzs和zsgg,密碼用md5加密
htpasswd?-c?-m?/etc/nginx/htpasswd?lzs htpasswd?-m?/etc/nginx/htpasswd?zsgg
????????4、重啟服務
????????5、客戶端訪問http://x.x.x.x/nginx-status即可
????3、使用limit_rate限制客戶端傳輸數據的速度
????????? ?1、編輯/etc/nginx/nginx.conf
location?/?{root????/var/www/nginx;index????index.html;limit_rate????2k;????????#對每個連接的限速為2k/s
????????????2、重啟服務
注意要點:
????1、配置文件中的每個語句要以;結尾
????2、使用htpasswd命令需要先安裝httpd
轉載于:https://blog.51cto.com/lzs66/1844964