I have an nginx installation, with three sites (but 4 configs including default):
Actual domain replaced with example.com.
default, a.example.com (a.example.conf), example.com (example.conf) & z.example.com (z.example.conf)
They appear in that order in the sites-available and sites-enabled directories. All 3 have a records in my DNS, to the same IP.
My problem is, I cannot seem to get the subdomains to actually resolve their sites in the /var/www/html/ folder, they are both instantly redirected to the main domain (example.com, they dont just show that site - they actually redirect to it). I beleive it must have something to do with my configuration file, which is odd but has been built to work (so far!) with phpBB3 installed in a subdirectory, with other site files outside of the subdirectory.
They configs for the subdomains must be getting picked up because there is no blanket catch and re-direct, if I enter somethingrandom.example.com into my browser then I get "This site can’t be reached" as i would expect.
default is the default nginx file.
a.example.conf
server {
# Listen IPv4 & IPv6
listen xxx.xx.xxx.xx:80;
listen [xxxx:xxxx::xxxx:xxxx:xxxx:xxxx]:80;
# Server
server_name a.example.com www.a.example.com;
root /var/www/html/a-example/;
index index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location ~* \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_connect_timeout 300s;
fastcgi_read_timeout 300s;
fastcgi_send_timeout 300s;
}
}
example.conf
#HTTPS and WWW rewrite
server {
# Listen IPv4 & IPv6
listen xxx.xx.xxx.xx:80;
listen [xxxx:xxxx::xxxx:xxxx:xxxx:xxxx]:80;
# Server
server_name www.example.com example.com;
# Rewrite to https and www
rewrite ^ https://www.example.com$request_uri? permanent;
}
# HTTPS with no WWW rewrite - couldnt get this to work too many redirects, use if in next block for now.
#server {
# Listen IPv4 & IPv6
#listen xxx.xx.xxx.xx:443 ssl http2;
#listen [xxxx:xxxx::xxxx:xxxx:xxxx:xxxx]:443 ssl http2;
# Server
#server_name example.com;
#include /etc/nginx/example-ssl.conf;
#rewrite ^ https://www.example.com$request_uri? permanent;
#}
# HTTPS with WWW final
server {
# Listen IPv4 & IPv6
listen xxx.xx.xxx.xx:443 ssl http2;
listen [xxxx:xxxx::xxxx:xxxx:xxxx:xxxx]:443 ssl http2;
# Server
server_name www.example.com;
# Includes
include /etc/nginx/example-main.conf;
include /etc/nginx/example-ssl.conf;
if ($host = 'example.com') {
return 301 https://www.example.com$request_uri;
}
# Config
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_connect_timeout 300s;
fastcgi_read_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_param HTTPS on;
}
}
example-main.conf
# Root Directory
root /var/www/html/example/public;
# phpBB uses index.htm
index index.php index.html index.htm;
# Loggers
error_log /var/log/nginx/example.com.error.log warn;
access_log /var/log/nginx/example.com.access.log;
# Error Pages
error_page 400 /error.php?mode=400;
error_page 401 /error.php?mode=401;
error_page 403 /error.php?mode=403;
error_page 404 /error.php?mode=404;
error_page 500 /error.php?mode=500;
# Favicon
location = /favicon.ico {
log_not_found off;
access_log off;
}
# Robots
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Site Assets
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
# Restrict compressed file size
location ~* ^.+\.(zip|rar|7z){
client_max_body_size 100M;
}
# Deny Cache Access
location /forums/cache/ {
deny all;
internal;
}
# Deny access to version control system directories.
location ~ /\.svn|/\.git {
deny all;
internal;
}
# Allow only internal access to download files
location /downloads/file/ {
internal;
}
# Site
location / {
try_files $uri $uri/ =404;
#location ~ \.php(/|$) {
#include fastcgi.conf;
#fastcgi_split_path_info ^(.+\.php)(/.*)$;
#fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
#fastcgi_param DOCUMENT_ROOT $realpath_root;
# try_files $uri $uri/ /app.php$is_args$args;
#fastcgi_pass unix:/run/php/php7.4-fpm.sock;
#}
}
# Forums SubDir
location /forums/ {
try_files $uri $uri/ @rewriteapp;
# Pass the php scripts to FastCGI server specified in upstream declaration.
location ~ \.php(/|$) {
include fastcgi.conf;
# phpBB defaults
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
try_files $uri $uri/ forums/app.php$is_args$args;
#custom
#fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param HTTP_PROXY '';
#try_files $uri $uri/ /forums/app.php$is_args$args $document_root$fastcgi_script_name =404;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
# Deny access to internal phpbb files.
location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb(?!\w+)|store|vendor) {
deny all;
# deny was ignored before 0.8.40 for connections over IPv6.
# Use internal directive to prohibit access on older versions.
internal;
}
}
# Rewrite to app.php method
location @rewriteapp {
rewrite ^(.*)$ /forums/app.php/$1 last;
}
# Wordpress Shop
location /shop/ {
try_files $uri $uri/ /shop/index.php?$args;
}
example-ssl.conf
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
ssl_stapling on;
ssl_stapling_verify on;
ssl_session_cache shared:SSL:10m;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;
add_header Strict-Transport-Security "max-age=15768000";
keepalive_timeout 70;
a.example.conf
server {
listen 80;
listen [::]:80;
server_name z.example.com www.z.example.com;
root /var/www/html/zexample;
index index.php;
try_files $uri /index.php;
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
I have kept the include structure in place to show my files as I am unsure if it could be effecting the problem!
I'm not sure if its related, but the app.php rewrite within php in some places does not work either, for example if it has url parameters following it in the request. Also the /shop/
part of example.conf doesnt seem to work - the main wordpress page shows however any other page or link after /shop/
hits a 404 page on Phpbb3. Again, these issues may not be related but I thought i'd include them!
Many thanks for any and all assistance.