php第2課:用nginx反向代理

php第2課:用nginx反向代理

時間 2019-04-01

主講?劉培富

地點?在線分享,大家自學


前一課,搭建了php的服務器環(huán)境,沒有這一課,服務器也可以正常使用了。但是,很多情況,會遇一些難題,而nginx可以解決這些難題,讓網站的服務器環(huán)境更加可靠、更加安全、管理更加便利。

1,什么是nginx?

nginx是一款高性能的web反向代理服務(無緩存反向代理),支持5萬個并發(fā)響應,可以做簡單的負載均衡(輕量級)和容錯。

一般情況下,IIS在并發(fā)達到200個時,就出現明顯的請求排隊現象。

使用nginx的優(yōu)勢:方便配置ssl,負載均衡,反爬蟲

官網:

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

建議下載1.15版本

nginx-1.15.8.zip? ? 大小:1.5MB


2,nginx.conf文件的配置:

只需要配置這一個文件即可。

nginx.conf包含三部分,即:全局塊、events塊、http塊。

http塊,又包含兩部分:全局塊,server塊。server塊可以有多個。

舉例(nginux v1.15.8),范本如下:

#user nobody;

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 {

? ? 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? 0;

? ? keepalive_timeout? 65;

? ? #gzip? on;

? ? log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_Host"';

? ? server {

? ? ? ? listen? ? ? 1600;

? ? ? ? server_name? bjn.cn;

? ? ? ? #charset koi8-r;

? ? ? ? #access_log? logs/host.access.log? main;

? ? ? ? location / {

? ? ? ? ? ? root? E:\web;

? ? ? ? ? ? index? index.asp index.html index.htm;

? ? ? ? }

? ? ? ? error_page? 404? =? /error/error.html;

? ? ? ? #redirect server error pages to the static page /error/error2.html

? ? ? ? #

? ? ? ? error_page? 500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? error;

? ? ? ? }

? ? ? ? location ~ \.asp$ {? ? ? ?

? ? ? ? ? ? proxy_pass? http://10.0.11.6:1601;

if ($http_user_agent ~* "YisouSpider|Spider4") {

return 403;

}

? ? ? ? }

? ? ? ? error_log? ? logs/error20190212.log? ? error;

? ? ? ? access_log? ? logs/access20190212.log? ? main;

? ? ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80

? ? ? ? #

? ? ? ? #location ~ \.php$ {

? ? ? ? #? ? proxy_pass? http://127.0.0.1;

? ? ? ? #}

? ? ? ? # 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

? ? ? ? #

? ? ? ? #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 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;

? ? #? ? }

? ? #}

}

3,使用方法(命令):

cd到目錄,start nginx啟動

nginx -v查看Nginx版本

nginx -s stop 停用Nginx

nginx -s quit 停用Nginx(處理完正在進行中請求后停用)

nginx -s reload 重新加載配置,重啟進程

nginx -s reopen 重啟日志文件

4,利用user_agent進行反爬蟲

常見網頁返回狀態(tài)碼如下:

200 #請求成功,即服務器返回成功301 #永久重定向302 #臨時重定向403#禁止訪問,一般是服務器權限拒絕

400?#錯誤請求,請求中有語法問題,或不能滿足請求。

403?#服務器權限問題導致無法顯示

404 #服務器找不到用戶請求的頁面500#服務器內部錯誤,大部分是服務器的設置或內部程序出現問題

501 #沒有將正在訪問的網站設置為瀏覽器所請求的內容

502 #網關問題,是代理服務器請求后端服務器時,后端服務器不可用或沒有完成 相應網關服務器,這通常是反向代理服務器下面的節(jié)點出問題導致的。503 #服務當前不可用,可能是服務器超載或停機導致的,或者是反向代理服務器后面沒有可以提供服務的節(jié)點。504#網關超時,一般是網關代理服務器請求后端服務器時,后端服務器沒有在指定的時間內完成處理請求,多數是服務器過載導致沒有在特定的時間內返回數據給前端代理服務器。

505 #該網站不支持瀏覽器用于請求網頁的HTTP協(xié)議版本(最為常見的是HTTP/1.1)

5,隱藏nginx的版本號:

日志記錄,詳細教程參考:

https://www.cnblogs.com/czlun/articles/7010591.html

經過前面的設置和調試,利用nginx已實現:

1,反向代理,端口監(jiān)聽和服務,多端口監(jiān)聽(iis和apache同時)

2,記錄訪問日志和錯誤日志

3,SSL,即https訪問

4,錯誤轉向

5,反爬蟲策略

本課程的內容,需要對網站服務器部署有一定基礎,重在動手調試。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容