现在我直接使用下面的配置在我的Magento多站点中使用nginx。一切正常,但我不喜欢这种配置没有优化,因此非常庞大。如您所见,2个服务器配置几乎相同,只有server_name,ssl和fastcgi_param MAGE_RUN_CODE不同。如何加入2个服务器配置?正如你所看到的,这里有很多重复。
server {
listen 80;
server_name test.com test1.com;
rewrite / $scheme://www.$host$request_uri permanent; ## Forcibly prepend a www
}
server {
listen 80;
## SSL directives might go here
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/test/cert.cer;
ssl_certificate_key /etc/nginx/ssl/private/test.key;
server_name www.test.com test.com;
root /var/www/test;
location / {
index index.html index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
expires 30d; ## Assume all files are cachable
}
## These locations would be hidden by .htaccess normally
location ^~ /app/ { deny all; }
location ^~ /includes/ { deny all; }
location ^~ /lib/ { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/ { deny all; }
location ^~ /report/config.xml { deny all; }
location ^~ /var/ { deny all; }
location /var/export/ { ## Allow admins only to view export folder
auth_basic "Restricted"; ## Message shown in login window
auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
autoindex on;
}
location /. { ## Disable .htaccess and other hidden files
return 404;
}
location @handler { ## Magento uses a common front handler
rewrite / /index.php;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9000;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
error_page 403 /error-page.html;
error_page 502 /error-page.html;
error_page 503 /error-page.html;
error_page 504 /error-page.html;
}
server {
listen 80;
## SSL directives might go here
listen 443 ssl;
server_name test1.com www.test1.com;
root /var/www/test;
location / {
index index.html index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
expires 30d; ## Assume all files are cachable
}
## These locations would be hidden by .htaccess normally
location ^~ /app/ { deny all; }
location ^~ /includes/ { deny all; }
location ^~ /lib/ { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/ { deny all; }
location ^~ /report/config.xml { deny all; }
location ^~ /var/ { deny all; }
location /var/export/ { ## Allow admins only to view export folder
auth_basic "Restricted"; ## Message shown in login window
auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
autoindex on;
}
location /. { ## Disable .htaccess and other hidden files
return 404;
}
location @handler { ## Magento uses a common front handler
rewrite / /index.php;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9000;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE test1; ## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
error_page 403 /error-page.html;
error_page 502 /error-page.html;
error_page 503 /error-page.html;
error_page 504 /error-page.html;
}
答案 0 :(得分:1)
我考虑过为你重写整个配置文件,但是,如果你只是简单地从网上复制粘贴一些内容并且也可能违反StackOverflow应答格式的精神,那将是一个坏主意。
所以,在一般意义上,让我给你一些很好的指示,说明你如何能够自己完成你需要的东西。
另外,一般来说,就nginx和性能而言,拥有多个几乎相同的server
指令并不是一个坏主意 - 从管理的角度来看它们只是不好的,它们不是很好向某人看清楚服务器之间的差异。
include
您可以使用include
指令将每个服务器分隔为单独的文件。通过这种方式,任何查看您的设置的人都可以轻松使用diff
清楚地看到服务器之间的区别,并且您也可以对其中一个服务器文件进行更改,然后将所述更改应用于其他人{ {3}},这也将我们带到了根本原因......
您可以使用include
在一个给定文件中包含每个server
中的所有相同部分,其中每个server
将是完全准确的,并且只会列出配置之间的实际差异
patch
if
中使用各种指令通常不起作用,但使用中间变量可以实现相同的目的。include
和set
set
?如果您希望将整个内容放在一个server
中,则可以有条件地使用set
:
set $mage_rc "default";
if ($server_name = test1.com) {
set $mage_rc "test1";
}
location ~ \.php$ {
…
fastcgi_param MAGE_RUN_CODE $mage_rc;
…
}
根据您提供的信息,我可能会将服务器分开(即仍然使用单独的server
指令),在每个server
中定义一些局部变量,并include
两个服务器共有的配置,其中所述通用配置中将使用$mage_rc
之类的变量,这些变量应该在每个server
内定义。如果使用单独的server
,则无需使用上述if
- 您只需在每个server
内单独定义每个变量,一次针对整个server
上下文。< / p>
答案 1 :(得分:0)
这取决于你正在使用的发行版,在Ubuntu中,/etc/nginx/nginx.conf
文件在http {}部分中添加此条目include /etc/nginx/sites-enabled/*;
然后在包含/etc/nginx/sites-enabled/
希望有所帮助。