常用nginx web配置

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了常用nginx web配置脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

图片描述

常用nginx对于web项目配置整理,做个笔记。

php web项目配置:

server {
  listen 80;
  listen [::]:80;

  # 设置上传最大为5MB
  client_max_body_size 5m;

  root /srv/www/wechat/public;

  index index.htML index.php;

  server_name example.COM;

  location / {
    try_files $uri $uri/ =404;
  }

  # 支持php
  location ~ .php$ {
    # Check that the PHP script exists before passing IT
    try_files $fastcgi_SCRIPT_NAME =404;

    fastcgi_index index.php;
    include fastcgi.conf;

    # With php-FPM (or other unix sockets):
    fastcgi_pass unix:/VAR/run/php/php7.2-fpm.sock;
    #  # With PHP-CGI (or other tcp sockets):
    #  fastcgi_pass 127.0.0.1:9000;
  }

  # deny access to .htaccess files, if apache's document root
  # concurs with nginx's one
  #
  location ~ /.ht {
    deny all;
  }
}

php web项目配置 支持laravel、symfony、Yii2单入口:

...
  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    #try_files $uri $uri/ =404;  # 注释上面这句,使用下面这句
    try_files $uri $uri/ /index.php?$query_string;
  }
...

php web项目配置 支持ThinkPHP

...
  location ~ .php$ {
    # regex to split $uri to $fastcgi_script_name and $fastcgi_path
    fastcgi_split_path_info ^(.+.php)(/.+)$;

    # Check that the PHP script exists before passing it
    try_files $fastcgi_script_name =404;

    # Bypass the fact that try_files resets $fastcgi_path_info
    # see: http://trac.nginx.org/nginx/ticket/321
    set $path_info $fastcgi_path_info;
    fastcgi_param PATH_INFO $path_info;   # ThinkPHP依赖PATH_INFO这个环境变量

    fastcgi_index index.php;
    include fastcgi.conf;

    # With php-fpm (or other unix sockets):
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    #  # With php-cgi (or other tcp sockets):
    #  fastcgi_pass 127.0.0.1:9000;
  }
...

php web项目配置 禁止访问上传目录下的php文件:

...
  # 这个块location要放在 location ~ .php$ 之前
  location ~ ^/uploads/.*.php$ {     # 所有/uploads文件目录下的.php文件都被禁止访问
    #deny all;  # 返回403
    return 404; #返回404
  }
...

原文连接:
常用nginx web配置

脚本宝典总结

以上是脚本宝典为你收集整理的常用nginx web配置全部内容,希望文章能够帮你解决常用nginx web配置所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。