在我的nGinxconfiguration中避免重复的代码

我有一个WordPress的网站与PHP APCcaching启用,我想encryption所有请求http://www.domain.com/wp-login.php*http://www.domain.com/dashboard?action=profile 。 我想强制一个未encryption的连接一切。

我可以得到这个工作,但configuration是非常丑陋的黑客一起。 我的问题是我仍然必须通过我想要encryption到PHP引擎的两个string。 有一个更好的方法来做到这一点,而不是重复这么多。

 server { listen 443; ssl on; ssl_certificate /etc/nginx/ssl/new/server.crt; ssl_certificate_key /etc/nginx/ssl/new/server.key; server_name www.domain.com domain.com; root /var/www; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$args; } location ~ \wp-login.php.* { ##Stuff to send requests to the PHP engine here } location ~ \dashboard?action=profile.* { ##Stuff to send requests to the PHP engine here } location ~ \.php$ { ##Stuff to send requests to the PHP engine here } } server { listen 80; server_name www.domain.com domain.com; root /var/www; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$args; } location ~ \wp-login.php.* { ##Stuff to send requests to the PHP engine here } location ~ \dashboard?action=profile.* { ##Stuff to send requests to the PHP engine here } location ~ \.php$ { ##Stuff to send requests to the PHP engine here } location ~ \dashboard/\?action\=profile$ { rewrite ^ https://$http_host$request_uri? permanent; } location ~ \wp-login.php.* { rewrite ^ https://$http_host$request_uri? permanent; } } 

就要求SSLlogin和仪表板访问而言,有许多解决scheme。 一个简单的方法是编辑wp-config.php然后在/* That's all, stop editing! Happy blogging. */行的上面/* That's all, stop editing! Happy blogging. */ /* That's all, stop editing! Happy blogging. */ /* That's all, stop editing! Happy blogging. */ add define('FORCE_SSL_ADMIN', true);

就个人而言,我不担心滥用网站的https版本,因为search引擎默认会链接到非https版本的网站,所以除非您的用户经常login,否则只有很小一部分会使用https版本的网站,因此可以忽略不计的服务器负载增加,但YMMV。

您可能会在这篇Ars Technica文章“ Web Served”中find许多技巧,第5部分:您自己的博客 ,很有用。

您也可能想要查看nginx.org上常见的configuration陷阱 。

为了避免nginx重复的位置块使用nginx包括或使用嵌套的位置块 – 一个嵌套的位置块示例如下…

  location / { proxy_pass http://mywebfeservers; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $http_host; proxy_set_header X-Request-ID $uuid; proxy_set_header Via $via; location /aaa { # proxy_pass is not inherited, unsure about proxy_http_version proxy_pass http://mywebfeservers; proxy_http_version 1.1; # Prevent caching if_modified_since off; } } 

这里所有的位置都有这些不同的标题集。 只有/ aaa位置可以防止caching,但是仍然使用相同的头文件而不重复configuration。 可悲的是,你必须重复代理传递,因为inheritance不能与代理传递指令(因为我不知道)的原因。