分享Nginx服务器configuration

我怎样才能分享两台服务器之间的通用configuration。 我的应用程序支持http和https(几页),我目前使用fastcgi_param来保存敏感信息,如数据库名称和密码。 我怎样才能分享两个服务器(80,443)的位置和fastcgi_param。


服务器{
    听80;
     server_name example.com;
 }

服务器{
    听443 ssl;
     server_name example.com;
     root /home/forge/example.com/public;

     #FORGE SSL(请勿删除!)
     ssl;
     ssl_certificate /etc/nginx/ssl/example.com/304/server.crt;
     ssl_certificate_key /etc/nginx/ssl/example.com/304/server.key;

     index index.html index.htm index.php;

     charset utf-8;

    位置 / {
         try_files $ uri $ uri / /index.php?$query_string;
     }

     location = /favicon.ico {access_log off;  log_not_found off;  }
     location = /robots.txt {access_log off;  log_not_found off;  }

     access_logclosures;
     error_log /var/log/nginx/example.com-error.log错误;

     error_page 404 /index.php;

    位置〜\ .php $ {
         fastcgi_param ENV“生产”;
         fastcgi_param DB_HOST“127.0.0.1”;
         fastcgi_param DB_PASSWORD“123456”;
         fastcgi_param DB_USERNAME“user”;
         fastcgi_param DB_NAME“example”;
         fastcgi_split_path_info ^(。+ \。php)(/.+)$;
         fastcgi_pass unix:/var/run/php5-fpm.sock;
         fastcgi_index index.php;
        包括fastcgi_params;
     }

     location〜/\.ht {
        否认一切;
     }
 }

conf我想分享:

 index index.html index.htm index.php;

     charset utf-8;

    位置 / {
         try_files $ uri $ uri / /index.php?$query_string;
     }

     location = /favicon.ico {access_log off;  log_not_found off;  }
     location = /robots.txt {access_log off;  log_not_found off;  }

     access_logclosures;
     error_log /var/log/nginx/example.com-error.log错误;

     error_page 404 /index.php;

    位置〜\ .php $ {
         fastcgi_param ENV“生产”;
         fastcgi_param DB_HOST“127.0.0.1”;
         fastcgi_param DB_PASSWORD“123456”;
         fastcgi_param DB_USERNAME“user”;
         fastcgi_param DB_NAME“example”;
         fastcgi_split_path_info ^(。+ \。php)(/.+)$;
         fastcgi_pass unix:/var/run/php5-fpm.sock;
         fastcgi_index index.php;
        包括fastcgi_params;
     }

     location〜/\.ht {
        否认一切;
     }

从0.7.14开始,您可以将HTTP和HTTPS服务器块合并为一个 – 更容易维护:

server { listen 80; listen 443 ssl; server_name example.com; ... } 

有关详细信息,请参阅http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server

除了安德烈的答案,这应该是非常有帮助的。

NGINX也支持include语句。

你可以例如创建一个公共目录( / etc / nginx / common / ),然后创建/etc/nginx/common/locations.conf 。 你的locations.conf文件将包含类似的东西,

 # NGINX CONFIGURATION FOR COMMON LOCATION # Basic locations files location = /favicon.ico { access_log off; log_not_found off; expires max; } # Cache static files location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|swf)$ { add_header "Access-Control-Allow-Origin" "*"; access_log off; log_not_found off; expires max; } # Security settings for better privacy # Deny hidden files location ~ /\.well-known { allow all; } location ~ /\. { deny all; access_log off; log_not_found off; } # Deny backup extensions & log files location ~* ^.+\.(bak|log|old|orig|original|php#|php~|php_bak|save|swo|swp|sql)$ { deny all; access_log off; log_not_found off; } # Return 403 forbidden for readme.(txt|html) or license.(txt|html) or example.(txt|html) if ($uri ~* "^.+(readme|license|example)\.(txt|html)$") { return 403; } 

然后在你的一个站点配置文件中使用include common/locations.conf; 包括位置文件。 例如,

 server { listen 80; listen 443 ssl; server_name example.com; include common/locations.conf; ... }