|
| 1 | +# Configuring web servers: Nginx |
| 2 | + |
| 3 | +To use [Nginx](https://wiki.nginx.org/), install PHP as an [FPM SAPI](https://secure.php.net/install.fpm). |
| 4 | +Use the following Nginx configuration, replacing `path/to/app/public` with the actual path for |
| 5 | +`app/public` and `mysite.test` with the actual hostname to serve. |
| 6 | + |
| 7 | +```nginx |
| 8 | +server { |
| 9 | + charset utf-8; |
| 10 | + client_max_body_size 128M; |
| 11 | +
|
| 12 | + listen 80; ## listen for ipv4 |
| 13 | + #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 |
| 14 | +
|
| 15 | + server_name mysite.test; |
| 16 | + root /path/to/app/public; |
| 17 | + index index.php; |
| 18 | +
|
| 19 | + access_log /path/to/basic/log/access.log; |
| 20 | + error_log /path/to/basic/log/error.log; |
| 21 | +
|
| 22 | + location / { |
| 23 | + # Redirect everything that isn't a real file to index.php |
| 24 | + try_files $uri $uri/ /index.php$is_args$args; |
| 25 | + } |
| 26 | +
|
| 27 | + # uncomment to avoid processing of calls to non-existing static files by Yii |
| 28 | + #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { |
| 29 | + # try_files $uri =404; |
| 30 | + #} |
| 31 | + #error_page 404 /404.html; |
| 32 | +
|
| 33 | + # deny accessing php files for the /assets directory |
| 34 | + location ~ ^/assets/.*\.php$ { |
| 35 | + deny all; |
| 36 | + } |
| 37 | +
|
| 38 | + location ~ \.php$ { |
| 39 | + include fastcgi_params; |
| 40 | + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
| 41 | + fastcgi_pass 127.0.0.1:9000; |
| 42 | + #fastcgi_pass unix:/var/run/php8-fpm.sock; |
| 43 | + try_files $uri =404; |
| 44 | + } |
| 45 | +
|
| 46 | + location ~* /\. { |
| 47 | + deny all; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +When you use this configuration, also set `cgi.fix_pathinfo=0` in the `php.ini` file |
| 53 | +to avoid many unnecessary system `stat()` calls. |
| 54 | + |
| 55 | +Also, note that when running an HTTPS server, you need to add `fastcgi_param HTTPS on;` so that Yii |
| 56 | +can detect if a connection is secure. |
0 commit comments