1
0
Files
frost-zx.github.io/docs/content/nginx-config-snippet.md
2025-10-13 10:20:34 +08:00

3.2 KiB
Raw Permalink Blame History

title, date, lastmod, tags
title date lastmod tags
Nginx 常用配置文件片段 2025-03-15T22:57:21Z 2025-03-15T22:59:02Z
配置
Nginx
服务器
网站

Nginx 常用配置文件片段

宝塔单域名子目录站点

可直接在 “伪静态” 内填写;假设域名为 “www.example.com”。

# 注意 alias 和 root 的区别

## 访问:`http://www.example.com/example_1/index.html`
## 实际:`/www/wwwroot/example_site/example_1/index.html`
## 注意:不要漏掉末尾的 `/`,以免产生目录穿越漏洞
location ^~ /example_1/ {
  root /www/wwwroot/example_site;
  # 可选默认与“server”中指定的一致
  index index.php index.html;
  # 启用 PHP仅在使用“root”时可用
  include /www/server/nginx/conf/enable-php-74.conf;
}

## 访问:`http://www.example.com/example_2/index.html`
## 实际:`/www/wwwroot/example_site/example_2/index.html`
location ^~ /example_2/ {
  alias /www/wwwroot/example_site/example_2/;
  index index.php index.html;
}

enable-php-74.conf 文件中的内容(该文件自带,不需要创建或修改):

location ~ [^/]\.php(/|$) {
  try_files $uri =404;
  fastcgi_pass  unix:/tmp/php-cgi-74.sock;
  fastcgi_index index.php;
  include fastcgi.conf;
  include pathinfo.conf;
}

负载均衡

upstream example_servers {
  server 192.168.1.101:8080 weight=30;
  server 192.168.1.102:8080 weight=20;
  server 192.168.1.103:8080 weight=10;
  # weight 可选,表示权重,值越大则被分配到的几率越大
}

server {
  listen 80;
  server_name www.example.com;
  location / {
    proxy_pass http://example_servers;
  }
}

禁止访问

禁止访问部分扩展名文件

location ~ \.(conf|ini|php)$ {
  deny all;
}

禁止访问目录

location ^~ /example/ {
  deny all;
}

添加跨域响应头

location / {
  # add_header 'Access-Control-Allow-Origin' '$http_origin';
  add_header 'Access-Control-Allow-Origin' '*';
  add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, POST';
  add_header 'Access-Control-Allow-Headers' 'Cache-Control, Content-Type, User-Agent, X-Requested-With';
  add_header 'Access-Control-Allow-Credentials' 'true';

  if ($request_method = 'OPTIONS') {
    return 204;
  }
}

一个 server 配置多个域名

server_name 中以空格分隔多个域名:

server {
  listen 80;
  server_name a.frost-zx.top b.frost-zx.top;
  ...
}

一个 server 配置多个类型的 SSL 证书

参考:Module ngx_http_ssl_module

注意:只有 OpenSSL 1.0.2 及以上版本支持

ssl_certificatessl_certificate_key,复制多一份,然后把路径修改为其他类型证书的路径:

server {
  listen              443 ssl;
  server_name         example.com;

  ssl_certificate     example.com.rsa.crt;
  ssl_certificate_key example.com.rsa.key;

  ssl_certificate     example.com.ecdsa.crt;
  ssl_certificate_key example.com.ecdsa.key;

  ...
}

域名重定向

server {

  ...

  if ($host ~ '^www.example.com') {
    return 301 https://test.example.com/;
  }

  ...

}