時間:2023-07-01 21:09:02 | 來源:網(wǎng)站運營
時間:2023-07-01 21:09:02 來源:網(wǎng)站運營
Nginx使用:#啟動子進程程序默認用戶#user nobody;#一個主進程和多個工作進程。工作進程是單進程的,且不需要特殊授權(quán)即可運行;這里定義的是工作進程數(shù)量worker_processes 1;#全局錯誤日志的位置及日志格式#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { #每個工作進程最大的并發(fā)數(shù) worker_connections 1024;}#http服務(wù)器設(shè)置http { #設(shè)定mime類型,類型由mime.type文件定義 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"'; #$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址; #$remote_user:用來記錄客戶端用戶名稱; #$time_local: 用來記錄訪問時間與時區(qū); #$request: 用來記錄請求的url與http協(xié)議; #$status: 用來記錄請求狀態(tài);成功是200, #$body_bytes_sent :記錄發(fā)送給客戶端文件主體內(nèi)容大??; #$http_referer:用來記錄從那個頁面鏈接訪問過來的; #$http_user_agent:記錄客戶瀏覽器的相關(guān)信息; #全局訪問日志路徑 #access_log logs/access.log main; #sendfile指令指定 nginx 是否調(diào)用sendfile 函數(shù)(zero copy 方式)來輸出文件,對于普通應(yīng)用,必須設(shè)為on。如果用來進行下載等應(yīng)用磁盤IO重負載應(yīng)用,可設(shè)置為off,以平衡磁盤與網(wǎng)絡(luò)IO處理速度,降低系統(tǒng)uptime。 sendfile on; #此選項可以減少網(wǎng)絡(luò)報文段的數(shù)量 #tcp_nopush on; #長連接超時時間 #keepalive_timeout 0; keepalive_timeout 65; #開啟壓縮 #gzip on; #配置虛擬主機 server { #虛擬主機使用的端口 listen 80; #虛擬主機域名 server_name localhost; #虛擬主機支持的字符集 #charset koi8-r; #虛擬主機的訪問日志路徑 #access_log logs/host.access.log main; #定義web根路徑 location / { #根目錄路徑 root html; #索引頁 index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #根據(jù)錯誤碼 返回對應(yīng)的頁面 error_page 500 502 503 504 /50x.html; #定義頁面路徑 location = /50x.html { root html; } #定義反向代理服務(wù)器 數(shù)據(jù)服務(wù)器是lamp模型 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ /.php$ { # proxy_pass http://127.0.0.1; #} #定義PHP為本機服務(wù)的模型 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ /.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #拒絕nginx DR目錄及子目錄下的.htaccess文件訪問 #location ~ //.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} #https的配置方案 # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}
server { listen 80; server_name localhost; location / { root html; index index.html index.htm; #支持目錄瀏覽 autoindex on; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}
[root@localhost ~]# mkdir /usr/local/nginx/html/a[root@localhost html]# vim /usr/local/nginx/conf/nginx.conflocation /a { autoindex on; deny 192.168.11.116; allow all; #基于客戶端IP做過濾,符合條件的允許訪問,不符合的禁止訪問 }[root@localhost ~]# killall -s HUP nginx
[root@localhost ~]# mkdir /usr/local/nginx/html/c[root@localhost html]# vim /usr/local/nginx/conf/nginx.conflocation /c { auth_basic "登陸驗證"; auth_basic_user_file /etc/nginx/htpasswd;}[root@localhost ~]# killall -s HUP nginx
log_format格式變量: $remote_addr #記錄訪問網(wǎng)站的客戶端地址 $remote_user #遠程客戶端用戶名 $time_local #記錄訪問時間與時區(qū) $request #用戶的http請求起始行信息 $status #http狀態(tài)碼,記錄請求返回的狀態(tài)碼,例如:200、301、404等 $body_bytes_sent #服務(wù)器發(fā)送給客戶端的響應(yīng)body字節(jié)數(shù) $http_referer #記錄此次請求是從哪個連接訪問過來的,可以根據(jù)該參數(shù)進行防盜鏈設(shè)置。 $http_user_agent #記錄客戶端訪問信息,例如:瀏覽器、手機客戶端等 $http_x_forwarded_for #當前端有代理服務(wù)器時,設(shè)置web節(jié)點記錄客戶端地址的配置,此參數(shù)生效的前提是代理服務(wù)器也要進行相關(guān)的x_forwarded_for設(shè)置
自定義一個json格式的訪問日志log_format main_json '{ "@timestamp":"$time_local", "client_ip":"$remote_addr", "request":"$request", "status":"$status", "bytes":"$body_bytes_sent", "x_forwarded":"$http_x_forwarded_for", "referer":"$http_referer" }'; access_log logs/access_json.log main_json;#重新啟動nginx服務(wù)查看訪問日志[root@localhost~]# tail -f /usr/local/nginx/logs/access_json.log
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conflocation / { root html; index index.html index.htm; } #定義訪問資源為圖片類型的location ~* /.(gif|jpg|png|bmp)$ { #定義白名單 none代表直接訪問的,blocked表示被防火墻標記過的請求最后一個是網(wǎng)址 valid_referers none blocked *.baidu.com; #如果不在白名單中則返回403 if ($invalid_referer) { return 403; } }[root@localhost ~]# killall -s HUP nginx #在另外一臺主機寫一個盜鏈測試頁面dnf install httpd -yvim /var/www/html/index.html<html> <head> <title>ce shi</title> <body> <a href="http://192.168.11.16/1.png">daolian</a> </body> </head></html>systemctl restart httpd
server { listen 192.168.11.251:80; location / { root html/web1; index index.html index.htm index.php; }}server { listen 192.168.11.252:80;location / { root html/web2; index index.html index.htm; }}
基于端口server { listen 80; #server_name www.abc.com; location / { root html/web1; index index.html index.htm index.php; }}server { listen 8080; #server_name www.abc.com; location / { root html/web2; index index.html index.htm; }}
基于域名server { listen 80; server_name web1.a.com; location / { root html/web1; index index.html index.htm index.php; }}server { listen 80; server_name web2.b.com; location / { root html/web2; index index.html index.htm; }}
location / {index index.php index.html index.htm; #定義首頁索引文件的名稱proxy_pass http://mysvr ;#請求轉(zhuǎn)向mysvr 定義的服務(wù)器列表}
反向代理優(yōu)化proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數(shù)client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù),proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時時間(代理連接超時)proxy_send_timeout 90; #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時)proxy_read_timeout 90; #連接成功后,后端服務(wù)器響應(yīng)時間(代理接收超時)proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置proxy_busy_buffers_size 64k; #高負荷下緩沖大?。╬roxy_buffers*2)proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳
算法思想是: 水(請求)從上方倒入水桶,從水桶下方流出(被處理); 來不及流出的水存在水桶中(緩沖),以固定速率流出; 水桶滿后水溢出(丟棄)。 這個算法的核心是:緩存請求、勻速處理、多余的請求直接丟棄。 相比漏桶算法,令牌桶算法不同之處在于它不但有一只“桶”,還有個隊列,這個桶是用來存放令牌的,隊列才是用來存放請求的。
#基于IP對下載速率做限制 限制每秒處理1次請求,對突發(fā)超過5個以后的請求放入緩存區(qū) http { limit_req_zone $binary_remote_addr zone=test:10m rate=1r/s; server { location /abc { limit_req zone=test burst=5 nodelay; }}limit_req_zone $binary_remote_addr zone=test:10m rate=1r/s;第一個參數(shù):$binary_remote_addr 表示通過remote_addr這個標識來做限制,“binary_”的目的是縮寫內(nèi)存占用量,是限制同一客戶端ip地址。第二個參數(shù):zone=test:10m表示生成一個大小為10M,名字為test的內(nèi)存區(qū)域,用來存儲訪問的頻次信息。第三個參數(shù):rate=1r/s表示允許相同標識的客戶端的訪問頻次,這里限制的是每秒1次,還可以有比如30r/m的。limit_req zone=test burst=5 nodelay;第一個參數(shù):zone=test 設(shè)置使用哪個配置區(qū)域來做限制,與上面limit_req_zone 里的name對應(yīng)。第二個參數(shù):burst=5,重點說明一下這個配置,burst爆發(fā)的意思,這個配置的意思是設(shè)置一個大小為5的緩沖區(qū)當有大量請求(爆發(fā))過來時,超過了訪問頻次限制的請求可以先放到這個緩沖區(qū)內(nèi)。第三個參數(shù):nodelay,如果設(shè)置,超過訪問頻次而且緩沖區(qū)也滿了的時候就會直接返回503,如果沒有設(shè)置,則所有請求會等待排隊。重啟nginx服務(wù)測試機[root@slave tmp]# for i in `seq 1 10`; do (wget http://192.168.11.16/abc/bigfile -P /tmp) & done[root@slave tmp]# killall wget 會出現(xiàn)不同的狀態(tài),有的是以退出有的是已終止
2)限制并發(fā)連接數(shù)#基于IP做連接限制 限制同一IP并發(fā)為1limit_conn_zone $binary_remote_addr zone=addr:10m;server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /abc { limit_conn addr 1; } }}重啟nginx服務(wù)測試機[root@slave tmp]# for i in `seq 1 10`; do (wget http://192.168.11.16/abc/bigfile -P /tmp) & done#退出了9個只能開啟一個
3)限制下載速度下載速度為100kserver { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /abc { limit_rate 100k; } }}重啟nginx服務(wù)測試機使用wget命令測試
4)綜合案例http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65;#基于IP做連接限制 限制同一IP并發(fā)為1 下載速度為100Klimit_conn_zone $binary_remote_addr zone=addr:10m;#基于IP對下載速率做限制 限制每秒處理1次請求,對突發(fā)超過5個以后的請求放入緩存區(qū) limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /abc { limit_req zone=one burst=5 nodelay; limit_conn addr 4; limit_rate 100k; } }}
將http://www.a.com 重寫為 http://www.a.com/hellolocation / { set $name hello; rewrite ^(.*)$ http://www.a.com/$name; }重啟nginx服務(wù)測試機打開瀏覽器可以看到頁面跳轉(zhuǎn)
if 指令 負責判斷location / { root html; index index.html index.htm; if ($http_user_agent ~* 'Firefox') { return 403; } }重啟nginx客戶機打開火狐瀏覽器測試,看看能否看到403
return 指令 定義返回數(shù)據(jù)location / { root html; index index.html index.htm; if ($http_user_agent ~* 'Firefox') { return 403; } }
域名跳轉(zhuǎn)www.a.com 重寫為 www.b.comserver { listen 80; server_name www.a.com; location / { rewrite ^/$ http://www.b.com permanent; }}
redirect標志:臨時重定向域名跳轉(zhuǎn)www.a.com 重寫為 www.b.comserver { listen 80; server_name www.a.com; location / { rewrite ^/$ http://www.b.com redirect; }}
break標志: 類似臨時重定向域名跳轉(zhuǎn)www.a.com 重寫為 www.b.comserver { listen 80; server_name www.a.com; location / { rewrite ^/$ http://www.b.com break; }}
last標志:如果是firefox瀏覽器 就將 http://192.168.11.16/$URI 重寫為 http://192.168.11.16/firefox/$URI實現(xiàn) 步驟1)URL重寫2)請求轉(zhuǎn)給本機locationlocation / {.....if ($http_user_agent ~* 'firefox'){ #^ 以什么開頭 ^a #$ 以什么結(jié)尾 c$ #. 除了回車以外的任意一個字符 #* 前面的字符可以出現(xiàn)多次或者不出現(xiàn) #更多內(nèi)容看正則表達式 rewrite ^(.*)$ /firefox/$1 last; } location /firefox { root html ; index index.html; }}[root@localhost html]# pwd/usr/local/nginx/html[root@localhost html]# mkdir firefox[root@localhost html]# echo firefox > firefox/index.html[root@localhost html]# killall nginx[root@localhost html]# /usr/local/nginx/sbin/nginx 客戶機瀏覽器測試
啟動工作進程數(shù)量worker_processes 4;#指定運行的核的編號,采用掩碼的方式設(shè)置編號worker_cpu_affinity 0001 0010 0100 1000;events {單個工作進程維護的請求隊列長度 worker_connections 1024;}
keepalive_timeout 0; 0代表關(guān)閉#keepalive_timeout 100;#keepalive_requests 8192;
gzip on;gzip_proxied any;gzip_min_length 1k;gzip_buffers 4 8k;gzip_comp_level 6;gzip_types text/plain text/css application/x-javascript application/javascript application/xml;# 開啟gzip gzip off; # 啟用gzip壓縮的最小文件,小于設(shè)置值的文件將不會壓縮 gzip_min_length 1k; # gzip 壓縮級別,1-9,數(shù)字越大壓縮的越好,也越占用CPU時間,后面會有詳細說明 gzip_comp_level 1; # 進行壓縮的文件類型。javascript有多種形式。其中的值可以在 mime.types 文件中找到。 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; # 是否在http header中添加Vary: Accept-Encoding,建議開啟 gzip_vary on; # 禁用IE 6 gzip gzip_disable "MSIE [1-6]/."; # 設(shè)置壓縮所需要的緩沖區(qū)大小 gzip_buffers 32 4k; # 設(shè)置gzip壓縮針對的HTTP協(xié)議版本 gzip_http_version 1.0;
expires指令:開啟緩存并指定靜態(tài)緩存時間location ~* /.(png|gif)$ { expires 1h; }
[root@client ~]# dnf -y install nginx php* mariadb-server mariadb -y
[root@client ~]# vim /etc/php-fpm.d/www.conf user = nginxgroup = nginx#php-fpm服務(wù)默認以apache用戶啟動,將啟動用戶身份修改nginx
[root@client ~]# systemctl restart nginx.service php-fpm.service mariadb.service
[root@client ~]# echo "<?php phpinfo();?>" >> /usr/share/nginx/html/index.php#系統(tǒng)自帶的nginx的頁面文件存放在/usr/share/nginx/html/目錄中
[root@client ~]# mysqladmin -u root password 123456[root@client ~]# mysql -u root -pEnter password: MariaDB [(none)]> create database wordpress charset=utf8;Query OK, 1 row affected (0.001 sec)MariaDB [(none)]> quitBye
[root@client ~]# cd /usr/share/nginx/html/[root@client html]# rm -fr *#####刪除nginx自帶的頁面文件,以及剛才測試生成的php頁面[root@client ~]# unzip latest-zh_CN.zip [root@client ~]# cd wordpress/[root@client wordpress]# mv * /usr/share/nginx/html/####將wordpress文件移動到nginx頁面文件存放的目錄[root@client wordpress]# cd /usr/share/nginx/html/[root@client html]# chown -R nginx.nginx *###文件的默認所有者是root,為了避免權(quán)限的問題,將所有者改為nginx
復(fù)制提示頁面內(nèi)容,按照要求手工創(chuàng)建wp-config.php文件并將內(nèi)容粘貼[root@client html]# vim /usr/share/nginx/html/wp-config.php
關(guān)鍵詞:使用
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。