Home  >  Q&A  >  body text

Extract common parts of server configuration in nginx

As follows:

server {
    listen       80;
    server_name  dev.drpb.com;
    root /Users/Stone/repo/oschina/drsoft/page-builder;
    location ~ \.php$ {
        fastcgi_index  index.php;
        fastcgi_pass  127.0.0.1:9000;
        include fastcgi.conf;
    }
}
server {
    listen       80;
    server_name  dev.drp.com;
    root /Users/Stone/repo/oschina/drsoft/site;
    location ~ \.php$ {
        fastcgi_index  index.php;
        fastcgi_pass  127.0.0.1:9000;
        include fastcgi.conf;
    }
}

There are two server configuration blocks in the nginx configuration, and they have a common php reverse proxy configuration part.

How to add:

location ~ \.php$ {
    fastcgi_index  index.php;
    fastcgi_pass  127.0.0.1:9000;
    include fastcgi.conf;
}

Extract it and put it in one place instead of writing it once in each server (I tried putting it in the http block of its common parent but it didn't work), thank you!

过去多啦不再A梦过去多啦不再A梦2713 days ago469

reply all(1)I'll reply

  • 阿神

    阿神2017-05-16 17:18:37

    Answer found:

    1. Extract the location part to an external file, for example, name it: common_rules.conf, and put it in the nginx directory.

    2. Modify the server configuration to:

    server {
        server_name  dev.drpb.com;
        root /Users/Stone/repo/oschina/drsoft/page-builder;
        include common_rules.conf;
    }
    server {
        server_name  dev.drp.com;
        root /Users/Stone/repo/oschina/drsoft/site;
        include common_rules.conf;
    }

    reply
    0
  • Cancelreply