2014-06-11 5 views
2

어떻게 두 서버간에 공통 구성을 공유 할 수 있습니다. 내 애플 리케이션은 HTTP 및 https (몇 페이지)를 지원하며 현재 DB 이름과 암호 같은 중요한 정보를 저장하기 위해 fastcgi_param을 사용하고 있습니다. 두 서버 (80, 443)에 대해 위치와 fastcgi_param을 어떻게 공유 할 수 있습니까?공유 Nginx 서버 구성

 

server { 
    listen 80; 
    server_name example.com; 
} 

server { 
    listen 443 ssl; 
    server_name example.com; 
    root /home/forge/example.com/public; 

    # FORGE SSL (DO NOT REMOVE!) 
    ssl on; 
    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; 

    location/{ 
     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_log off; 
    error_log /var/log/nginx/example.com-error.log error; 

    error_page 404 /index.php; 

    location ~ \.php$ { 
     fastcgi_param ENV "production"; 
     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; 
     include fastcgi_params; 
    } 

    location ~ /\.ht { 
     deny all; 
    } 
} 

의 conf 내가 공유하려는 :

 
index index.html index.htm index.php; 

    charset utf-8; 

    location/{ 
     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_log off; 
    error_log /var/log/nginx/example.com-error.log error; 

    error_page 404 /index.php; 

    location ~ \.php$ { 
     fastcgi_param ENV "production"; 
     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; 
     include fastcgi_params; 
    } 

    location ~ /\.ht { 
     deny all; 
    } 
+0

는 http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server 나는 http://nginx.org/en (당신은 당신이 필요이 –

+0

은 [포함]해야 할 것 같아요 /docs/ngx_core_module.html#include) 서버 블록에있는 파일. – vutran

답변

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; 

    ... 
}