0

Inside /etc/nginx/sites-available, I have a config file example with the following configuration:

server { listen 443 ssl; server_name php1.example.com php2.example.com; ssl_certificate /etc/nginx/ssl/example.crt; ssl_certificate_key /etc/nginx/ssl/example.key; root /var/www/html/example/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust the PHP version if needed fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } error_log /var/log/nginx/example_error.log; access_log /var/log/nginx/example_access.log; } server { listen 80; server_name admin.php1.example.com; root /var/www/html/example/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust the PHP version if needed fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } error_log /var/log/nginx/example_error.log; access_log /var/log/nginx/example_access.log; } 

This serves a PHP website which is setup in a way to connect to different databases for different subdomains. I am using Cloudflare and could not setup SSL for the multi-level subdomain admin.php1.example.com. I have pointed php1.example.com, php2.example.com, php3.example.com and admin.php1.example.com to my server's IP, but have not included php3.example.com in my config file.

The issue is, when I go to php3.example.com in my browser, it loads up the website even though it is not configured in the config file. I cannot figure out which config file is being used to serve the website.

Since it was working as intended for the other subdomains, I ignored it.

Until now, when I had to setup a Node.js project on the same server. I wrote another config file node-project.conf with the following configuration:

server { listen 80; server_name node.example.com; # location / { # proxy_pass http://localhost:4000; # proxy_http_version 1.1; # proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection 'upgrade'; # proxy_set_header Host $host; # proxy_cache_bypass $http_upgrade; # } location / { root /usr/share/nginx/html; index index.html index.htm; } } 

I am trying to serve NGINX's default welcome page to test if my new subdomain node.example.com is correctly pointed to my IP. But when I visited the subdomain in my browser, I got served the same PHP website that was being served for php3.example.com.

I am new to this, but am passionate about server administration. I want to fix this issue as well as understand what is happening behind the scene, so I can become a better admin.

I am also new to Server Fault. I have tried my best to make the question as descriptive as possible, but if anybody wants additional information regarding anything, please let me know.

In sites-available, I have default config with following configuration:

server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; server_name _; location / { try_files $uri $uri/ =404; } } 
1

1 Answer 1

0

You can use nginx -T. In my case it shows this

$ nginx -T | grep "# config*" nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # configuration file /etc/nginx/nginx.conf: # configuration file /etc/nginx/mime.types: # configuration file /etc/nginx/sites-enabled/default: 

Keep in mind that you need to symlink to sites-enabled to activate a conf file for nginx and reload


You can either grep through /etc/nginx recursively and see if you can find a match for any of the conf files which has a value php3.example.com

grep -r "php3.example.com" /etc/nginx 

Or only through sites-enabled and sites-available

grep -r "php3.example.com" /etc/nginx/sites-enabled /etc/nginx/sites-available 

You could enable access logging in the main nginx config (/etc/nginx/nginx.conf)

http { # Other configurations... log_format custom_format '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' 'vhost:$server_name'; # Enable logging access_log /var/log/nginx/access.log custom_format; error_log /var/log/nginx/error.log; # Other configurations... } 

And then you could grep through those logs if there is a hit for php3.example.com

grep 'php3.example.com' /var/log/nginx/access.log 
7
  • Can you please explain what does it do and how does it solve the problem? Commented Feb 21, 2024 at 7:00
  • Nginx -t is to test /show the running nginx config and then i grep the output for conf files Commented Feb 21, 2024 at 7:01
  • I ran nginx -t and it was okay. I also did the symlink thing. Commented Feb 21, 2024 at 7:03
  • nginx -T also generates output, each configuration file that is active has above it # configuration file Commented Feb 21, 2024 at 7:05
  • It shows there is nothing wrong with the config and a list of config files. What I want is to understand why is my issue occurring and a way to fix it. Commented Feb 21, 2024 at 8:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.