【问题标题】:Share Nginx server configuration分享 Nginx 服务器配置
【发布时间】:2014-08-01 00:15:08
【问题描述】:

如何在两台服务器之间共享通用配置。我的应用程序同时支持 http 和 https(对于几页),我目前正在使用 fastcgi_param 来保存数据库名称和密码等敏感信息。如何共享两个服务器(80、443)的位置和 fastcgi_param。

服务器 { 听 80; server_name example.com; } 服务器 { 听 443 ssl; server_name example.com; 根 /home/forge/example.com/public; # 伪造 SSL(请勿删除!) 开启ssl; ssl_certificate /etc/nginx/ssl/example.com/304/server.crt; ssl_certificate_key /etc/nginx/ssl/example.com/304/server.key; 索引 index.html index.htm index.php; 字符集 utf-8; 地点 / { try_files $uri $uri/ /index.php?$query_string; } 位置 = /favicon.ico { access_log off; log_not_found 关闭; } 位置 = /robots.txt { access_log off; log_not_found 关闭; } access_log 关闭; 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 "用户"; fastcgi_param DB_NAME "例子"; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; 包括 fastcgi_params; } 位置 ~ /\.ht { 全部否认; } }

我想分享的配置:

索引 index.html index.htm index.php; 字符集 utf-8; 地点 / { try_files $uri $uri/ /index.php?$query_string; } 位置 = /favicon.ico { access_log off; log_not_found 关闭; } 位置 = /robots.txt { access_log off; log_not_found 关闭; } access_log 关闭; 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 "用户"; fastcgi_param DB_NAME "例子"; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; 包括 fastcgi_params; } 位置 ~ /\.ht { 全部否认; }

【问题讨论】:

标签: php nginx


【解决方案1】:

从 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 了解详情。

【讨论】:

    【解决方案2】:

    除了安德烈的回答,它应该对你有很大帮助。

    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;
    
        ...
    }
    

    【讨论】:

      【解决方案3】:

      我个人使用Ansible 通过描述您想要的最终状态的数据文件来配置和设置服务器。看 https://github.com/geerlingguy/ansible-role-nginx

      requirements.yml

      ---
      - src: geerlingguy/ansible-role-nginx
      

      主机

      [local]
      localhost ansible_connection=local
      

      playbook.yml 伪代码

      ---
      - hosts: server
        roles:
          - { role: geerlingguy.nginx }
        nginx_vhosts:
        - listen: "80"
          server_name: "example.com www.example.com"
          return: "301 https://example.com$request_uri"
          filename: "example.com.80.conf"
      

      您可以使用Jinja2 templates 复制和配置片段

      使用ansible-galaxy -i hosts playbook.yml 运行它

      【讨论】:

        猜你喜欢
        • 2017-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 2013-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多