加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

linux – 如何使用nginx conf if/else语句?

发布时间:2020-12-13 21:25:45 所属栏目:Nginx 来源:网络整理
导读:是不是可以用nginx做这样的事情? if ( $http_user_agent = "wget" ){ server { listen 11.11.11.11:1111; root /website1/; server_name example.com www.example.com;else server { listen 22.22.22.22:22222; root /website2/; server_name example2.com

是不是可以用nginx做这样的事情?

if ( $http_user_agent = "wget" ){
   server {
      listen      11.11.11.11:1111;
      root        /website1/;
      server_name example.com www.example.com;
else 

   server {
      listen      22.22.22.22:22222;
      root        /website2/;
      server_name example2.com www.example2.com;
}
最佳答案
Nginx重写模块确实有if directive(参见链接文档中的示例),但不是.对于else的等价物将是所有未使用if修改的.

您可以在服务器{}内部使用if,但不能反过来使用.在您已经需要服务器的请求之前,您甚至不会在if中比较条件.在那里,您只能在用户代理是(或不是)wget时提供内容.

另请注意,如果位于内部位置,则不应使用,因为它可能无法正常工作(请参阅If Is Evil).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读