upstream apireport {
server 172.18.2.60:18083;
}
server {
listen 80;
listen 443 ssl http2;
server_name 123.com;
access_log logs/report.access.log;
error_log logs/report.error.log;
ssl_certificate cert/214602433430717.pem;
ssl_certificate_key cert/214602433430717.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#api-report
location / {
#允许所有域名跨域*
add_header Access-Control-Allow-Origin '*' always;
#允许客户端携带验证信息,例如 cookie 之类的
add_header Access-Control-Allow-Credentials true;
#允许跨域方法:例如get
add_header Access-Control-Allow-Methods 'GET, POST,PUT, OPTIONS,DELETE';
#允许跨域的请求头:例如token,假如开发多加一个请求头t123,下面Authorization后面新加上: ,t123
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,token,Authorization';
#遇到OPTIONS方法返回204,安全起见
if ($request_method = 'OPTIONS') {
return 204;
}
#显示真实ip
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
proxy_set_header Host $host;
#proxy_set_header cf-connecting-ip $remote_addr; #这行cf-connecting-ip这个可以是参数,传真实ip
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://apireport/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_connect_timeout 60s;
proxy_read_timeout 600s;
proxy_send_timeout 200s;
}
}
//请求日志记录,关键字段:request_time-请求总时间,upstream_response_time后端处理时 间
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host" "$cookie_ssl_edition" '
'"$upstream_addr" "$upstream_status" "$request_time" '
'"$upstream_response_time" ';
access_log logs/access.log main;
加 / 与不加 /
在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径
例如
1,加 /
server_name example.com
location /data/ {
proxy_pass http://127.0.0.1/;
}
访问 http://example.com/data/index.html
会转发到 http://127.0.0.1/index.html
2,不加 /
server_name example.com
location /data/ {
proxy_pass http://127.0.0.1;
}
访问 http://example.com/data/index.html
会转发到 http://127.0.0.1/data/index.html