1. #user nobody;
  2. worker_processes 1; # 工作进程个数,可配置多个
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024; # 单个进程最大连接数
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server { #每一个server相当于一个代理服务器
  23. listen 80; #监听端口,默认80
  24. server_name localhost; #当前服务的域名,可以有多个,用空格分隔(本地是localhost)
  25. root /usr/tomcat/webapps/test; #假如不配置的话,所有css,js,img文件都无法加载
  26. #charset koi8-r;
  27. #access_log logs/host.access.log main;
  28. location / { #表示匹配的路径,这时配置了/表示所有请求都被匹配到这里
  29. root html;
  30. index index.html index.htm;
  31. }
  32. #error_page 404 /404.html;
  33. # redirect server error pages to the static page /50x.html
  34. #
  35. error_page 500 502 503 504 /50x.html;
  36. location = /50x.html {
  37. root html;
  38. }
  39. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  40. #
  41. #location ~ \.php$ {
  42. # proxy_pass http://127.0.0.1; #请求转向自定义的服务器列表
  43. #}
  44. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  45. #
  46. #location ~ \.php$ {
  47. # root html;
  48. # fastcgi_pass 127.0.0.1:9000;
  49. # fastcgi_index index.php;
  50. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  51. # include fastcgi_params;
  52. #}
  53. # deny access to .htaccess files, if Apache's document root
  54. # concurs with nginx's one
  55. #
  56. #location ~ /\.ht {
  57. # deny all;
  58. #}
  59. }
  60. # another virtual host using mix of IP-, name-, and port-based configuration
  61. #
  62. #server {
  63. # listen 8000;
  64. # listen somename:8080;
  65. # server_name somename alias another.alias;
  66. # location / {
  67. # root html;
  68. # index index.html index.htm;
  69. # }
  70. #}
  71. # HTTPS server
  72. #
  73. #server {
  74. # listen 443 ssl;
  75. # server_name localhost;
  76. # ssl_certificate cert.pem;
  77. # ssl_certificate_key cert.key;
  78. # ssl_session_cache shared:SSL:1m;
  79. # ssl_session_timeout 5m;
  80. # ssl_ciphers HIGH:!aNULL:!MD5;
  81. # ssl_prefer_server_ciphers on;
  82. # location / {
  83. # root html;
  84. # index index.html index.htm;
  85. # }
  86. #}
  87. }

location的匹配规则

  1. 语法规则: location [=|~|~*|^~] /uri/ { }
  2. = #用于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停止
  3. ~ #用于标准uri前,表示包含正则表达式并且区分大小写
  4. ~* #用于标准uri前,表示包含正则表达式并且不区分大写
  5. !~ #用于标准uri前,表示包含正则表达式并且区分大小写不匹配
  6. !~*   #用于标准uri前,表示包含正则表达式并且不区分大小写不匹配
  7. ^~ #用于标准uri前,表示包含正则表达式并且匹配以什么开头
  8. $ #用于标准uri前,表示包含正则表达式并且匹配以什么结尾
  9. \ #用于标准uri前,表示包含正则表达式并且转义字符。可以转. * ?等
  10. * #用于标准uri前,表示包含正则表达式并且代表任意长度的任意字符
  11. 匹配优先级:=, ^~, ~/~*,/
  12. location优先级:(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)