在 nginx 配置文件中,重复性配置的复用方法

比如在一个 nginx 服务器中配置了多个二级域名,可能有一些关于 SSL 和其它安全 Header 的配置是大量重复的

每次添加或修改的话,都需要手动复制或替换很多地方,容易疏漏

可以将相同的配置统一写在一个文件中,在各处使用 include 引用即可。

1、新建一个文件,将所需配置存入其中(如保存至文件 /home/shared.conf)

listen              443 ssl;
listen              [::]:443 ssl;
root                e:/projects/TheVegCat/src/main/resources/static/;
ssl_certificate     e:/projects/TheVegCat/ssl/CA/localhost/localhost.crt;
ssl_certificate_key e:/projects/TheVegCat/ssl/CA/localhost/localhost.decrypted.key;
add_header          Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
gzip                on;
gzip_min_length     512;
gzip_proxied        expired no-cache no-store private auth;
gzip_types          text/plain application/xml text/css application/javascript application/json;
location ~ ^(.+)\.(ico|png|xml|json)$ {
    expires         1y;
    alias           e:/projects/TheVegCat/src/main/resources/static/favicon/;
    try_files       $1.$2 $1.$2/;
}

2、在 nginx.conf 中,使用 include 引用即可(路径不限)

server {
    server_name         localhost thevegcat.lan;
    include /home/shared.conf;
    location / {
        add_header      Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
        proxy_pass      http://localhost:8080;
    }
}

server {
    server_name         veganskivodic.thevegcat.lan;
    include /home/shared.conf;
    location / {
        add_header      Strict-Transport-Security "max-age=10368000; includeSubDomains" always;
        proxy_pass      http://veganskivodic.thevegcat.lan:8080;
    }
}

参考:https://serverfault.com/questions/1125363/avoid-duplicate-nginx-server-definition

posted @   不是豆豆  阅读(368)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2017-03-21 在 Visual Studio 2017 中找回消失的“在浏览器中查看”命令
友情链接:迷途


点击右上角即可分享
微信分享提示