阅读量:0
在 Nginx 配置中,proxy_pass
指令中的路径会影响到请求的转发方式。当你在 location
中定义了 /api
路径时,如果你在 proxy_pass
中也包含了 /api
,那么请求将会按照这样的方式进行转发:
假设你在 Nginx 中配置了如下的 location
和 proxy_pass
:
location /api { proxy_pass http://backend.server; }
当前端发起请求 www.example.com/api/data
时,Nginx 将会将这个请求转发到 http://backend.server/api/data
。
这种行为是由于在 proxy_pass
中如果包含了路径,如 /api
在这里,Nginx 将保留原始请求中的 URI (Uniform Resource Identifier),除非你在 proxy_pass
中指定了一个新的 URI 来覆盖这个行为。这对于像 API Gateway 这样需要转发或者修改请求的服务来说非常有用。
如果你希望在转发请求时不包括类似 /api
这样的路径段,你可以在 proxy_pass
中使用反斜杠 /
来覆盖这个行为,如下所示:
location /api { proxy_pass http://backend.server/; }
这样,当前端请求 www.example.com/api/data
时,Nginx 将会将请求转发到 http://backend.server/data
,而不会包含原始请求的路径段 /api
。
总之,proxy_pass
指令中的路径部分会影响请求的转发路径格式,进而影响后端服务器收到请求的 URI 结构。
全部配置
#user nobody; worker_processes 8; #error_log logs/error.log; #error_log logs/error.log notice; #crit #error_log logs/error.log info; error_log /usr/local/nginx/logs/nginx_error.log error; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 2048; } 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; tcp_nopush on; #仅依赖于sendfile使用,能够使得Nginx在一个数据包中尝试发送响应头,以及在数据包中发送一个完整文件 tcp_nodelay on; #启用或禁用TCP_NODELAY选项,用于keep-alive连接 #keepalive_timeout 0; keepalive_timeout 300; #gzip on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; #gzip_http_version 1.1; gzip_comp_level 5; gzip_types text/plain application/x-javascript application/javascript text/css application/xml image/jpeg image/gif image/png; #gzip_vary on; gzip_disable "MSIE [1-6]\."; #IE1-6版本不支持gzip压缩 proxy_connect_timeout 75; proxy_read_timeout 300; proxy_send_timeout 300; proxy_buffer_size 64k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; client_max_body_size 100m; server_tokens off; proxy_temp_path /usr/local/nginx/temp; #用于指定本地目录来缓冲较大的代理请求 #设定负载均衡的服务器列表 upstream myweb { #weigth参数表示权值,权值越高被分配到的几率越大 # ip_hash; server 172.26.5.6:5210 weight=1 max_fails=2 fail_timeout=30s; server 172.26.5.6:6610 weight=1 max_fails=2 fail_timeout=30s; # server 172.26.136.135:4310 weight=1 max_fails=2 fail_timeout=30s; } upstream mp { server 172.26.5.6:2300 weight=1 max_fails=2 fail_timeout=30s; } upstream mylocal { #weigth参数表示权值,权值越高被分配到的几率越大 # ip_hash; server 172.26.5.6:3310 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; listen [::]:80; ## ipv6 support server_name _; #charset koi8-r; rewrite ^(.*) https://www.j.com$1 permanent; #access_log logs/host.access.log main; # location / { # proxy_set_header Host $proxy_host; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # access_log logs/webaccess.log main ; # client_max_body_size 20m; # proxy_read_timeout 1500; # client_body_buffer_size 256k; # proxy_buffer_size 64k; # proxy_buffers 4 64k; # proxy_busy_buffers_size 128k; # proxy_temp_file_write_size 256k; # proxy_pass http://myweb/; # } location /console/ { proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; access_log logs/consoleaccess.log main ; client_max_body_size 20m; proxy_read_timeout 1500; client_body_buffer_size 256k; proxy_buffer_size 64k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 256k; proxy_pass http://mylocal/; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { proxy_pass http://myweb; expires 7d; } location ~ .*\.(js|css)$ { proxy_pass http://myweb; expires 7d; } # redirect server error pages to the static page /50x.html # error_page 404 403 500 502 503 504 /404.html; location = /404.html { root /usr/local/nginx/html; index 404.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; #} } server { listen 443 ssl; listen [::]:443 ssl; # listen [::]:443; server_name j.com www.j.com; #ssl on; ssl_certificate /usr/local/nginx/crt/j.com.pem; ssl_certificate_key /usr/local/nginx/crt/j.com.key; ssl_protocols TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_stapling on; ssl_stapling_verify on; ssl_prefer_server_ciphers on; location / { proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; access_log logs/webaccess.log main ; client_max_body_size 20m; proxy_read_timeout 1500; client_body_buffer_size 256k; proxy_buffer_size 64k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 256k; proxy_pass http://myweb/; # } location /docs { root /app; charset utf-8; index index.htm index.html; } location /jlrcwpages { root /app; charset utf-8; index index.htm index.html; } location /ysxRtxF72U.txt { root /app/txt; charset utf-8; } location /api { proxy_pass https://hr.j.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 允许跨域请求 add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain; charset=utf-8'; add_header 'Content-Length' 0; return 204; } } } }