Skip to content

Nginx基础

后端服务接口

nginx
location /v1/ {
    proxy_pass http://127.0.0.1:8080/;
    proxy_read_timeout 30;
    proxy_redirect off;
    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 REMOTE-HOST $remote_addr;
}

最佳实践

/结束,请求转发到后端服务器后,不会携带 /v1/

访问日志

nginx
access_log /var/log/nginx/yourdomain.com.https.log;

请求体大小

nginx
client_max_body_size 20m;

微信授权

nginx
location ~ .*.(txt) {
    root /etc/nginx/text;
}

开启gzip

nginx
gzip on;
gzip_min_length 10k;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-8]\.";

静态资源缓存

nginx
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
}

http重定向https

一般设置了https之后,需要将之前的http请求重定向到https

nginx
server {
    listen 80;
    server_name yourdomain.com;
    rewrite ^(.*)$ https://$host$1 redirect;    # 临时重定向 302
    #rewrite ^(.*)$ https://$host$1 permanent;   # 永久重定向 301
}