nginx如何复用部分配置块?

例如下面这个配置块。我有N个PHP站点,他们除了server_name和root这个配置项不同以外,其他配置项都是一样的,有什么办法可以复用其他相同的配置吗?

另外请问一下,我下面这个配置项是否有安全隐患?如果有的话需要怎么修改呢?

非常感谢!^_^

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    
    root /var/www/html;
    
    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;
    
    server_name www.xxx.com;
    
    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }       
    
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }       
    
    location ~ /\.ht {
        deny all;
    }
}

阅读 4.5k
1 个回答

一般复用就用include语句就行了,暂时没太好的方法……
(写完Caddy配置再回来看Nginx的配置,尤其会有种想骂街的感觉。。)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题