- 什么是反向代理?
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,并將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個反向代理服務器。

- 怎么配置nginx實現以上目標?
- 環境參數:
nginx目錄:D:/nginx-1.14.2/
應用程序目錄:D:nginx-1.14.2html
Internet入口IP地址和端口:1.2.3.4:80
應用程序IP地址和端口:127.0.0.1:8088
2.nginx.conf 配置文件如下
注:conf / nginx.conf 是 nginx 的默認配置文件。你也可以使用 nginx -c 指定你的配置文件。
#運行用戶
#user nobody;
#啟動進程,通常設置成和cpu的數量相等
worker_processes 1;
#全局錯誤日志
error_log D:/nginx-1.14.2/logs/error.log;
error_log D:/nginx-1.14.2/logs/error.log notice;
error_log D:/nginx-1.14.2/logs/error.log info;
#PID文件,記錄當前啟動的nginx的進程ID
pid D:/nginx-1.14.2/logs/nginx.pid;
#工作模式及連接數上限
events {worker_connections 1024; #單個后臺worker process進程的最大并發鏈接數
}#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {#設定mime類型(郵件支持類型),類型由mime.types文件定義include D:/nginx-1.14.2/conf/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 D:/nginx-1.14.2/logs/access.log main;rewrite_log on;#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對于普通應用,#必須設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime.sendfile on;#tcp_nopush on;#連接超時時間keepalive_timeout 120;tcp_nodelay on;#gzip壓縮開關#gzip on;#設定實際的服務器列表 upstream zp_server1{server 127.0.0.1:8088;}#HTTP服務器server {#監聽80端口,80端口是知名端口號,用于HTTP協議listen 80;#定義使用www.xx.com訪問server_name 1.2.3.4;#首頁index index.html#指向webapp的目錄root D:nginx-1.14.2html;#編碼格式charset utf-8;#代理配置參數proxy_connect_timeout 180;proxy_send_timeout 180;proxy_read_timeout 180;proxy_set_header Host $host;proxy_set_header X-Forwarder-For $remote_addr;#反向代理的路徑(和upstream綁定),location 后面設置映射的路徑location / {proxy_pass http://zp_server1;} #設定查看Nginx狀態的地址location /NginxStatus {stub_status on;access_log on;auth_basic "NginxStatus";auth_basic_user_file conf/htpasswd;}#禁止訪問 .htxxx 文件location ~ /.ht {deny all;}#錯誤處理頁面(可選擇性配置)#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;}}
}
3.啟動應用程序
4.啟動nginx服務:
D:nginx-1.14.2>nginx.exe -c conf/nginx.conf
5.在瀏覽器中輸入1.2.3.4,就可以訪問了。