經常使用nginx web配置

圖片描述

經常使用nginx對於web項目配置整理,作個筆記。php

php web項目配置:html

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單入口:nginx

...
  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項目配置 支持ThinkPHPlaravel

...
  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文件:web

...
  # 這個塊location要放在 location ~ \.php$ 以前
  location ~ ^/uploads/.*\.php$ {     # 全部/uploads文件目錄下的.php文件都被禁止訪問
    #deny all;  # 返回403
    return 404; #返回404
  }
...

原文鏈接:
經常使用nginx web配置php7

相關文章
相關標籤/搜索