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

常用的前后端配置

配置1

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;
}

配置2

nginx
server {
    listen 80;
    server_name download.share888.top;
    
     # 静态资源访问 可选:单独配置静态资源(如需要)
    location /preview/upload/ {
    
        alias  /java_project/xuni/upload/;
        
        # 启用自动索引(按需开启)
        # autoindex on;
        
        # 设置缓存时间(按需配置)
        expires 30d;
        
        # 安全设置
        autoindex off;
        access_log off;
    }

    # 所有请求转发到后端
    location / {
        proxy_pass http://127.0.0.1:10001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    
    # 日志
    access_log  /java_project/xuni/download.share888.top.log;
    error_log  /java_project/xuni/download.share888.top.error.log;
   
}

强制https

比如我们想实现,即便是访问 http ,也要自动跳转到 https访问。

第一种方法

  • add_header Strict-Transport-Security: "max-age=31536000"
仅作用于当前域名,不包含子域名。例如配置在 example.com 时,
仅影响 example.com 的访问,不影响 a.example.com 或 b.example.com 等子域名。
  • add_header Strict-Transport-Security: "max-age=31536000"; includeSubDomains
同时作用于当前域名及其所有子域名。例如配置在 example.com 时,
会同时保护 a.example.com、b.example.com 等所有子域名。

两种配置都会强制浏览器在未来 max-age 指定的时间内(31536000秒=1年)仅通过HTTPS访问网站。

浏览器首次访问后会自动将HTTP请求转为HTTPS,即使用户手动输入 http:// 也会强制跳转。

第二种方法

nginx
server {
    listen 80;                                  # 监听HTTP默认端口80
    listen 443 ssl http2;
    server_name download.share888.top;          # 匹配域名download.share888.top的请求
    return 301 https://download.share888.top$request_uri;  
    # 返回301状态码,永久重定向到HTTPS的同路径URL
}
  1. 强制HTTPS跳转

    当用户通过 http://download.share888.top(明文HTTP)访问时,Nginx会立即返回 301 Moved Permanently 响应,将请求永久重定向到 https://download.share888.top(加密HTTPS)。

  2. 保留原始请求路径

与HSTS的关系

  1. 互补作用:

    此配置是 服务端强制跳转,而 Strict-Transport-Security 头(HSTS)是 客户端强制策略。两者结合使用效果更佳:

    • 用户首次访问时,通过301跳转到HTTPS。
    • 服务器在HTTPS响应中设置 Strict-Transport-Security 头,浏览器后续访问时会直接使用HTTPS(即使输入HTTP地址)。
  2. 区别:

    • 301跳转需要每次请求到服务端处理,而HSTS策略生效后,浏览器会直接跳过HTTP请求,减少一次潜在的不安全交互。

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