how nginx decide which server block to use

George / mzcart at qq.com
Wed Jun 21 07:26:10 UTC 2017


Hi all,


  i am running nginx version: nginx/1.12.0.i got following server block config as below, all request match regular expression work well,but request to server s01.example.com return 404.what's wrong? i googled for a while,most of the article said,it first try to match literal string ,then wildcard,and regular expression last. 


------------------------------
  server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/public_html;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ /\.ht {
      deny all;
    }
  }





server {
    listen [ip1]:80;
    server_name ~^(?<subdomain>[a-z0-9]+)\.(?<domain>[a-z0-9\-]+)\.(?<domext>[a-z]+);
    index index.html index.php;


    root /home/$domain.$domext/$subdomain;
    location / {
        try_files $uri $uri/ @apache =404;
    }


    location ~ (.*)\.html$ {
        if (!-f '$document_root/$uri') {
          rewrite /(.*)\.html$ /$1.php last;
        }
        try_files $uri @apache =404;
    }


    location @apache {
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ .*\.(php|php5|cgi|pl)$ {
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ /\.ht {
      deny all;
    }
}
 server {
    listen [ip2]:80;
    #server_name ~^(?<subdomain>[a-z0-9]+).(?<domain>[a-z0-9.]+);
    server_name ~^(?<subdomain>[a-z0-9]+)\.(?<domain>[a-z0-9\-]+)\.(?<domext>[a-z]+);
    #server_name ~^(?<subdomain>[a-z0-9]+).com;
    #access_log off;
    index index.html index.php;


    root /ip100/$domain.$domext/$subdomain;
    #add_header aa $document_root;
    location / {
        try_files $uri $uri/ @apache =404;
    }


    location ~ (.*)\.html$ {
        if (!-f '$document_root/$uri') {
          rewrite /(.*)\.html$ /$1.php last;
        }
        try_files $uri @apache =404;
    }


    location @apache {
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ .*\.(php|php5|cgi|pl)$ {
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ /\.ht {
      deny all;
    }
    #access_log  /home/wwwlogs/$subdomain.$domain.com_access.log access;
    #error_log  /home/wwwlogs/subdomain.$domain.com_error.log  error;
}


 server {
    listen [ip3]:80;
    server_name ~^(?<subdomain>[a-z0-9]+)\.(?<domain>[a-z0-9\-]+)\.(?<domext>[a-z]+);
    index index.html index.php;


    root /ip155/$domain.$domext/$subdomain;
    #add_header aa $document_root;
    location / {
        try_files $uri $uri/ @apache =404;
    }


    location ~ (.*)\.html$ {
        if (!-f '$document_root/$uri') {
          rewrite /(.*)\.html$ /$1.php last;
        }
        try_files $uri @apache =404;
    }


    location @apache {
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ .*\.(php|php5|cgi|pl)$ {
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ /\.ht {
      deny all;
    }
    #access_log  /home/wwwlogs/$subdomain.$domain.com_access.log access;
    #error_log  /home/wwwlogs/subdomain.$domain.com_error.log  error;
}




server {
  listen [ip3]:80;
  server_name s01.example.com;
  access_log off;
  index index.html index.htm index.php;
  root /data/ytginc.com/public;
  rewrite /([a-z]+)$ /index.php/$1;
  rewrite /([a-z0-9]+)/([a-z]+)/$ /index.php/$1/$2;


  location / {
    try_files $uri @apache;
  }
  location @apache {
include fastcgi_conf;
  }
  location ~ .*\.(php|php5|cgi|pl)?$ {
include fastcgi_conf;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
  }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
  }
  location ~ /\.ht {
    deny all;
  }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20170621/08c62aa6/attachment-0001.html>


More information about the nginx mailing list