Nginx正向代理和反向代理配置
生活随笔
收集整理的這篇文章主要介紹了
Nginx正向代理和反向代理配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
Nginx的反向代理功能應該是Nginx諸多功能里面最常用的一個功能了,正向代理的話可能使用的場景比較少,平時接觸的也不多,本章內容僅包含這兩個功能的基本使用配置,因為是本地版本的,所以不包含負載均衡相關的內容。完整配置和注釋
user root owner; worker_processes 4;#error_log /usr/local/etc/nginx/logs/error.log; #error_log /usr/local/etc/nginx/logs/info.log info;pid /Users/martin/nginx.pid;events {worker_connections 256; }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 /usr/local/etc/nginx/logs/access_log_pipe main;#sendfile on;#tcp_nopush on;keepalive_timeout 65;gzip on;#反向代理配置server {listen 443 ssl; #監聽443端口server_name app.doodl6.com; #服務域名ssl on; #是否開啟SSL加密ssl_certificate /Users/martin/Documents/ssl/doodl6.crt; # SSL加密證書ssl_certificate_key /Users/martin/Documents/ssl/doodl6.key; # SSL加密秘鑰charset UTF-8; #編碼指定location ~* ^.+\.(xls|woff2|log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip|js|css|less)$ { #代理指定后綴的請求,這里配的是常見的前端資源proxy_pass https://127.0.0.1:80; #轉向提供內容的真實服務器地址,也可以配置本地目錄(見HTTP代理配置)proxy_set_header Host $http_host; #寫入Header值,proxy_set_header referer "$http_referer";} location = / { #代理域名請求,也就只有域名的請求,如:https://app.doodl6.comproxy_pass https://127.0.0.1:8080;proxy_set_header Host $http_host;} location ~ / { #代理所有請求,不符合上面兩種配置的請求都會走這個代理配置proxy_pass http://127.0.0.1:8080;proxy_set_header Host $http_host;}}server {listen 80;server_name app.doodl6.com;charset UTF-8; location ~* ^.+\.(xls|woff2|log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip|js|css|less|ico)$ {expires 30s; #內容緩存30秒root /Users/martin/project/app/front; #指定文件根目錄} location ~ / {proxy_pass http://127.0.0.1:8080;proxy_set_header Host $http_host;}}#正向代理配置server{listen 82; #監聽端口 resolver 8.8.8.8; #DNSresolver_timeout 10s; # DNS解析超時時間location / {proxy_pass http://$http_host$request_uri;proxy_set_header Host $http_host;proxy_buffers 256 4k;proxy_max_temp_file_size 0;proxy_connect_timeout 30;proxy_cache_valid 200 302 10m;proxy_cache_valid 301 1h;proxy_cache_valid any 1m;}}#本地反向轉正向代理server {listen 80;server_name proxy.doodl6.com;charset UTF-8; location ~ / {proxy_pass http://127.0.0.1:82; #轉到本地正向代理proxy_set_header Host $http_host;}}}總結
以上是生活随笔為你收集整理的Nginx正向代理和反向代理配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx 正向代理(上网代理) 反向代
- 下一篇: Nginx —— 检查配置文件ngi