John Davidson

NGINX create subdomain that uses functions and assets from main Domain PHP

0 comments
Message:


I'm currently developing a subdomain (mobile version) of my main page, this is the structure:


mywebsite.com
├──App
│ ├──Controllers
│ │ └──testController.php
│ └──Models
│ └──testModel.php
└──public
├── css
│ └── style.css
├── jquery
│ └── jquery.js
├── js
│ ├── bootstrap.js
│ └── scripts.js
├── includes
│ └──something.php
├── mobile
│ └── index.php
└── index.php

I'm trying to configure the subdomain and domain in my NGINX. The idea is that mobile/index.php will be m.mywebsite.com and working with files in css, js and includes that are in main domain.


Currently, accessing to mywebsite.com/mobile/index.php works and loads functions and files, like testModel.php or style.css, etc that are outside of mobile folder. But doesn't works when I try to access from subdomain URL due to it tries to search those files inside mobile, and there is no way to search outside subdomain.


What options do I have? Should I create an html file with iframe that will be the subdomain and load the mobile/index.php?


There is a way in the virtualhost file to change the url presentation when inside mobile folder and make it look like m.mybesite.com?


This is the virtualhost file


server {
listen 80;
listen [::]:80;

root /var/www/test/mywebsite.com/public/mobile;

index index.html index.htm index.php;

server_name m.mywebsite.com;

location / {
try_files $uri $uri/ =404;
}
}
server {
server_name mywebsite.com;
root /var/www/test/mywebsite.com;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
}
}
server {
if ($host = mywebsite.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name mywebsite.com;
}


My Answer:
You can achieve this by configuring NGINX to serve the subdomain from the main domain's directory. Here's how you can modify your NGINX configuration:

nginx
server {
listen 80;
listen [::]:80;
root /var/www/test/mywebsite.com/public; # Serve files from the main domain directory
index index.html index.htm index.php;
server_name m.mywebsite.com;

location / {
try_files $uri $uri/ /mobile/index.php; # Serve mobile/index.php for subdomain requests
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Update this to match your PHP-FPM socket
}
}

server {
server_name mywebsite.com;
root /var/www/test/mywebsite.com;
index index.html index.php;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Update this to match your PHP-FPM socket
}

if ($host = mywebsite.com) {
return 301 https://$host$request_uri;
}

listen 80;
listen [::]:80;
}


With this configuration, requests to m.mywebsite.com will be served from the main domain's public directory, allowing you to access files and functions from the main domain in your subdomain. Make sure to adjust the PHP-FPM socket path to match your server configuration.

You don't need to use an iframe to load the mobile/index.php file. NGINX can handle the routing and serving of files for the subdomain directly.

Rate this post

5 of 5 based on 2008 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us