#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;
}
#代理mysql服務
stream {upstream mysql_backend {server 192.168.10.250:3306 weight=5 max_fails=3 fail_timeout=30s;}server {listen 9016;proxy_connect_timeout 10s;proxy_timeout 300s;proxy_pass mysql_backend;}
}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;#underscores_in_headers on;#gzip on;server {listen 9015;server_name localhost;# 主路徑代理(如/doc)location / {if ($request_method = 'OPTIONS') { return 200;}# 傳遞原始請求頭中的Authorization(Token)proxy_set_header Authorization $http_authorization;proxy_pass http://192.168.10.250:8080/examsystem/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header Authorization $http_authorization;# 重寫JS/CSS路徑(關鍵配置)#sub_filter '/js/' '/doc/js/';#sub_filter '/css/' '/doc/css/';#sub_filter_once off;}# 不需要Token的接口(如登錄接口)location ^~ /examsystem/itjavatemptable/itjavaTempTable { # 假設登錄接口路徑為/examsystem/loginproxy_pass http://192.168.10.250:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 不傳遞Authorization頭proxy_set_header Authorization "";}# 關鍵配置:重寫webjars路徑,靜態資源處理location ^~ /webjars/ {rewrite ^/webjars/(.*)$ /examsystem/webjars/$1 break;proxy_pass http://192.168.10.250:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}# 靜態資源代理(關鍵配置)#location /webjars/js/ {# alias http://192.168.10.250:8080/examsystem/webjars/js/; # 替換為后端實際路徑# 或使用root(需注意路徑拼接)# root /path/to/backend/examsystem/;#}#location /webjars/css/ {# alias http://192.168.10.250:8080/examsystem/webjars/css/;#} # 處理/examsystem/路徑下的所有靜態資源#location /examsystem/ {# proxy_pass http://192.168.10.250:8080/examsystem/;# proxy_set_header Host $host;# proxy_set_header X-Real-IP $remote_addr;# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# proxy_set_header X-Forwarded-Proto $scheme;#}# 可選:單獨處理常見靜態資源后綴location ~* \.(css|js|png|jpg|jpeg|gif|ico|woff|woff2|ttf|eot|svg)$ {proxy_pass http://192.168.10.250:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}# 緩存靜態資源(可選,提高性能)expires 7d;add_header Cache-Control "public";#charset koi8-r;#access_log logs/host.access.log main;#location / {# root html;# index index.html index.htm;#}#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;#}# 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;# }#}}
上面的nginx配置實現了nginx反向代理后端服務器接口及實現指定接口不進行token驗證的方法。本配置還代理了后端服務器的Mysql服務。