- 小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
前端开发中难免会碰到跨域问题,nginx配置反向代理就可以完美解决跨域问题,本文就记录一下nginx 的通用配置,当然nginx还有其它功能,后续也会记录一下。
1.nginx通用配置
worker_processes auto;
pid /tmp/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
#开启和关闭gzip模式
gzip on;
#gizp压缩起点,文件大于10k才进行压缩
gzip_min_length 10k;
# 配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
gzip_disable "MSIE [1-6]\.";
# 设置压缩所需要的缓冲区大小,以4k为单位,如果文件为7k则申请2*4k的缓冲区
gzip_buffers 2 4k;
# 设置gzip压缩针对的HTTP协议版本
gzip_http_version 1.0;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
gzip_comp_level 6;
# 进行压缩的文件类型。
gzip_types application/javascript text/css;
#nginx对于静态文件的处理模块,开启后会寻找以.gz结尾的文件,直接返回,不会占用cpu进行压缩,如果找不到则不进行压缩
gzip_static on;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
client_body_temp_path /tmp/client_body_temp;
proxy_temp_path /tmp/proxy_temp;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
tcp_nodelay on;
include /etc/nginx/conf.d/.upstream.conf;
# this is necessary for us to be able to disable request buffering in all cases
proxy_http_version 1.1;
log_format timed_combined '$remote_addr - '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time $upstream_response_time $pipe';
access_log /var/log/nginx/host.access.log timed_combined;
error_log /var/log/nginx/error.log error;
include /etc/nginx/conf.d/*.server.conf;
}
server {
listen 80;
server_name localhost;
client_max_body_size 100m;
client_body_buffer_size 256k;
client_body_temp_path /etc/nginx/proxy_temp;
include /etc/nginx/mime.types;
default_type application/octet-stream;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location ^~/aida_api {
# 后端 svc-name + 服务端口
proxy_pass http://xxxxxx:8080/aida_api;
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_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
}