Nginx Reverse Proxy and Cors

后端部署的服务有跨域,可以通过Nginx反向代理解决 nginx配置 将example.com:8888代理到example.com:8088 user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; server { listen 8888; server_name example.com; location / { add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Credentials' "true"; if ($request_method = "OPTIONS") { add_header 'Access-Control-Max-Age' 86400; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE'; add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type'; add_header 'Content-Length' 0; add_header 'Content-Type' 'text/plain, charset=utf-8'; return 204; } root /usr/share/nginx/html; index index....

March 23, 2023 · homfen

Express.js Get Client IP

Express中如果想获取客户端的IP,首先要设置trust proxy: app.set('trust proxy', 1); // or app.enable('trust proxy'); 设置完之后,就可以从req中取到IP了: var ip = req.ip; // or ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; ...

October 19, 2016