Skip to content
鼓励作者:欢迎打赏犒劳

02-NGINX常用配置

location

建议加上/

不加斜杠

shell
location /api {
    proxy_pass http://backend_server;
}
  • 客户端请求 /api/some-resource
  • Nginx 转发到 http://backend_server/api/some-resource

加斜杠

shell
location /api/ {
    proxy_pass http://backend_server/;
}
  • 客户端请求 /api/some-resource
  • Nginx 转发到 http://backend_server/some-resource

常用的前后端配置

nginx
server
{
    listen 80;
	listen 443 ssl http2;
    server_name my-api.share888.top;
    charset utf-8;
    index index.php index.html index.htm default.php default.htm default.html;

    #强制https,如果访问的不是https,则自动跳转到https
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }

    #gzip相关配置
    gzip  on;
    gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
    gzip_types *;
    gzip_vary on;
	  
    #配置https
    ssl_certificate /home/ssl/my-api.share888.top.crt; 
    ssl_certificate_key /home/ssl/my-api.share888.top.key; 
    
    # 前端资源
    location /pan/ {
      alias /panV3/dist/;
      index index.html;
    }
    # 静态资源
    location /preview/upload/ {
      alias /panV3/upload/;
    }
    # 后端接口
    location /my-api/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://127.0.0.1:8080/my-api/;
    }
    
    access_log  /www/wwwlogs/my-api.share888.top.log;
    error_log  /www/wwwlogs/my-api.share888.top.error.log;
}

如有转载或 CV 的请标注本站原文地址