09/04/2018 - LINUX, NGINX
This example shows us how to set multiple websites on different ports. We also changed default web root from /var/www/html
to /srv/www
.
srv
.
└── www
├── index.php # Default site root
├── phpinfo.php # Default site root
└── symfony
└── public
├── index.php # Symfony site root
└── phpinfo.php # Symfony site root
This is the "default" website configuration which uses port 80
.
$ cat /etc/nginx/sites-available/default
server {
listen 80 default_server;
root /srv/www;
server_name default.dev;
# Return 404 for all other files not matching the .php files
location / {
index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log /var/log/nginx/default_error.log;
access_log /var/log/nginx/default_access.log;
}
This is the "symfony" website configuration which uses port 81
.
$ cat /etc/nginx/sites-available/symfony
server {
listen 80; # Enables internal access without passing :81
listen 81; # Enables external access with passing :81
server_name symfony.dev;
root /srv/www/symfony/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/symfony_error.log;
access_log /var/log/nginx/symfony_access.log;
}
$ cat /etc/hosts
127.0.0.1 default.dev
127.0.0.1 symfony.dev
The actual address of the server is 192.168.99.30
.
You can use localhost
, 127.0.0.1
or default.dev
if you want to. Port 80
is optional for all hosts because this is the default site.
# Internal tests
$ curl 127.0.0.1 # serves index.php
$ curl 127.0.0.1/index.php
$ curl 127.0.0.1/phpinfo.php
$ curl 127.0.0.1:80 # serves index.php
$ curl 127.0.0.1:80/index.php
$ curl 127.0.0.1:80/phpinfo.php
# External tests from browsers
http://192.168.99.30/ # serves index.php
http://192.168.99.30/index.php
http://192.168.99.30/phpinfo.php
You can use localhost
, 127.0.0.1
or symfony.dev
if you want to. Port 81
is optional only for symfony.dev
but not others.
# Internal tests
$ curl 127.0.0.1:81 # serves index.php
$ curl 127.0.0.1:81/index.php
$ curl 127.0.0.1:81/phpinfo.php
$ curl symfony.dev # serves index.php
$ curl symfony.dev/index.php
$ curl symfony.dev/phpinfo.php
$ curl symfony.dev:81
$ curl symfony.dev:81/index.php
$ curl symfony.dev:81/phpinfo.php
# External tests from browsers (port is compulsory)
http://192.168.99.30:81/ # serves index.php
http://192.168.99.30:81/index.php
http://192.168.99.30:81/phpinfo.php