Skip to content

Commit 96c5005

Browse files
committed
minor #17887 Moving nginx above Apache (second attempt) (ThomasLandauer)
This PR was merged into the 5.4 branch. Discussion ---------- Moving nginx above Apache (second attempt) As requested at #17886 (comment) Commits ------- 7c1642c Moving nginx above Apache (second attempt)
2 parents 729fb78 + 7c1642c commit 96c5005

File tree

1 file changed

+90
-90
lines changed

1 file changed

+90
-90
lines changed

setup/web_server_configuration.rst

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,98 @@ to use PHP :ref:`with Nginx <web-server-nginx>`.
3030
another location (e.g. ``public_html/``) make sure you
3131
:ref:`override the location of the public/ directory <override-web-dir>`.
3232

33+
.. _web-server-nginx:
34+
35+
Nginx
36+
-----
37+
38+
The **minimum configuration** to get your application running under Nginx is:
39+
40+
.. code-block:: nginx
41+
42+
server {
43+
server_name domain.tld www.domain.tld;
44+
root /var/www/project/public;
45+
46+
location / {
47+
# try to serve file directly, fallback to index.php
48+
try_files $uri /index.php$is_args$args;
49+
}
50+
51+
# optionally disable falling back to PHP script for the asset directories;
52+
# nginx will return a 404 error when files are not found instead of passing the
53+
# request to Symfony (improves performance but Symfony's 404 page is not displayed)
54+
# location /bundles {
55+
# try_files $uri =404;
56+
# }
57+
58+
location ~ ^/index\.php(/|$) {
59+
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
60+
fastcgi_split_path_info ^(.+\.php)(/.*)$;
61+
include fastcgi_params;
62+
63+
# optionally set the value of the environment variables used in the application
64+
# fastcgi_param APP_ENV prod;
65+
# fastcgi_param APP_SECRET <app-secret-id>;
66+
# fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";
67+
68+
# When you are using symlinks to link the document root to the
69+
# current version of your application, you should pass the real
70+
# application path instead of the path to the symlink to PHP
71+
# FPM.
72+
# Otherwise, PHP's OPcache may not properly detect changes to
73+
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
74+
# for more information).
75+
# Caveat: When PHP-FPM is hosted on a different machine from nginx
76+
# $realpath_root may not resolve as you expect! In this case try using
77+
# $document_root instead.
78+
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
79+
fastcgi_param DOCUMENT_ROOT $realpath_root;
80+
# Prevents URIs that include the front controller. This will 404:
81+
# http://domain.tld/index.php/some-path
82+
# Remove the internal directive to allow URIs like this
83+
internal;
84+
}
85+
86+
# return 404 for all other php files not matching the front controller
87+
# this prevents access to other php files you don't want to be accessible.
88+
location ~ \.php$ {
89+
return 404;
90+
}
91+
92+
error_log /var/log/nginx/project_error.log;
93+
access_log /var/log/nginx/project_access.log;
94+
}
95+
96+
.. tip::
97+
98+
If you use NGINX Unit, check out the official article about
99+
`How to run Symfony applications using NGINX Unit`_.
100+
101+
.. note::
102+
103+
Depending on your PHP-FPM config, the ``fastcgi_pass`` can also be
104+
``fastcgi_pass 127.0.0.1:9000``.
105+
106+
.. tip::
107+
108+
This executes **only** ``index.php`` in the public directory. All other files
109+
ending in ".php" will be denied.
110+
111+
If you have other PHP files in your public directory that need to be executed,
112+
be sure to include them in the ``location`` block above.
113+
114+
.. caution::
115+
116+
After you deploy to production, make sure that you **cannot** access the ``index.php``
117+
script (i.e. ``http://example.com/index.php``).
118+
119+
For advanced Nginx configuration options, read the official `Nginx documentation`_.
120+
33121
.. _web-server-apache-mod-php:
34122

35-
Adding Rewrite Rules
36-
--------------------
123+
Adding Rewrite Rules for Apache
124+
-------------------------------
37125

38126
The easiest way is to install the ``apache`` :ref:`Symfony pack <symfony-packs>`
39127
by executing the following command:
@@ -292,94 +380,6 @@ instead:
292380
293381
FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.4-fpm.sock -pass-header Authorization
294382
295-
.. _web-server-nginx:
296-
297-
Nginx
298-
-----
299-
300-
The **minimum configuration** to get your application running under Nginx is:
301-
302-
.. code-block:: nginx
303-
304-
server {
305-
server_name domain.tld www.domain.tld;
306-
root /var/www/project/public;
307-
308-
location / {
309-
# try to serve file directly, fallback to index.php
310-
try_files $uri /index.php$is_args$args;
311-
}
312-
313-
# optionally disable falling back to PHP script for the asset directories;
314-
# nginx will return a 404 error when files are not found instead of passing the
315-
# request to Symfony (improves performance but Symfony's 404 page is not displayed)
316-
# location /bundles {
317-
# try_files $uri =404;
318-
# }
319-
320-
location ~ ^/index\.php(/|$) {
321-
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
322-
fastcgi_split_path_info ^(.+\.php)(/.*)$;
323-
include fastcgi_params;
324-
325-
# optionally set the value of the environment variables used in the application
326-
# fastcgi_param APP_ENV prod;
327-
# fastcgi_param APP_SECRET <app-secret-id>;
328-
# fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";
329-
330-
# When you are using symlinks to link the document root to the
331-
# current version of your application, you should pass the real
332-
# application path instead of the path to the symlink to PHP
333-
# FPM.
334-
# Otherwise, PHP's OPcache may not properly detect changes to
335-
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
336-
# for more information).
337-
# Caveat: When PHP-FPM is hosted on a different machine from nginx
338-
# $realpath_root may not resolve as you expect! In this case try using
339-
# $document_root instead.
340-
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
341-
fastcgi_param DOCUMENT_ROOT $realpath_root;
342-
# Prevents URIs that include the front controller. This will 404:
343-
# http://domain.tld/index.php/some-path
344-
# Remove the internal directive to allow URIs like this
345-
internal;
346-
}
347-
348-
# return 404 for all other php files not matching the front controller
349-
# this prevents access to other php files you don't want to be accessible.
350-
location ~ \.php$ {
351-
return 404;
352-
}
353-
354-
error_log /var/log/nginx/project_error.log;
355-
access_log /var/log/nginx/project_access.log;
356-
}
357-
358-
.. tip::
359-
360-
If you use NGINX Unit, check out the official article about
361-
`How to run Symfony applications using NGINX Unit`_.
362-
363-
.. note::
364-
365-
Depending on your PHP-FPM config, the ``fastcgi_pass`` can also be
366-
``fastcgi_pass 127.0.0.1:9000``.
367-
368-
.. tip::
369-
370-
This executes **only** ``index.php`` in the public directory. All other files
371-
ending in ".php" will be denied.
372-
373-
If you have other PHP files in your public directory that need to be executed,
374-
be sure to include them in the ``location`` block above.
375-
376-
.. caution::
377-
378-
After you deploy to production, make sure that you **cannot** access the ``index.php``
379-
script (i.e. ``http://example.com/index.php``).
380-
381-
For advanced Nginx configuration options, read the official `Nginx documentation`_.
382-
383383
.. _`Apache documentation`: https://httpd.apache.org/docs/
384384
.. _`FastCgiExternalServer`: https://docs.oracle.com/cd/B31017_01/web.1013/q20204/mod_fastcgi.html#FastCgiExternalServer
385385
.. _`Nginx documentation`: https://www.nginx.com/resources/wiki/start/topics/recipes/symfony/

0 commit comments

Comments
 (0)