Configure Nginx Sub Domain - nginx

I am trying to add a subdomain along side my existing website setup.
I have added the following server block to my nginx configuration located at /etc/nginx/sites-available/default.
server {
listen 80;
server_name stats.website.org;
root /var/www/piwik;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html/existing_website/public;
index index.php index.html;
server_name website.org;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am getting a ERR_NAME_NOT_RESOLVED error when I try stats.website.org.
Can anyone advise where I am going wrong.
Thanks in advance.

Related

Trying to serve wordpress in a subdirectory using NGINX webserver but failing

I have a react website which I am serving using NGINX. I wanted to create a blog for the same. So I tried to use wordpress in a sub-directory.
`
server {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name domain.com;
location / {
try_files $uri $uri/ =404;
}
location ^~ /blog {
client_max_body_size 10m;
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^/wordpress(/.+\.php)(.*)$;
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
#ssl certificates here
}
`
After hours of reading docs, blogs and stack I got my homepage set up. However all my pages on the blog are returning 404. I am attaching my nginx config.
My directory structure is
/var/www/html/ : root folder for my react website
/var/www/html/blog : root folder for my wordpress ( no /wordpress subfolder present)
Add this lines
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/blog
}

How to configure multiple sites with same URL on Nginx

I am trying to have 2 websites running on the same adress,
I want everything request on my.website.com/test_slim and every my.website.com/test_slim/anything/following to be directed on /var/www/html/test_slim/src/public/index.php.
I want everything else (my.website.com/, my.website.com/foo, my.website.com/bar/baz ...) to be served as normal PHP files in /var/www/html/whatever/file/according/to/url/index.php
Fow now, this are my server configurations :
In /etc/nginx/site-enabled/my-default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
In /etc/nginx/sites-enabled/test_slim
server {
listen 80;
server_name _;
index index.php;
error_log /var/www/html/test_slim/error.log;
access_log /var/www/html/test_slim/access.log;
root /var/www/html/test_slim/src/public;
location /test_slim/ {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
But what I actually get is :
Request on localhost/ execute /var/www/html/index.html as wanted.
Request on localhost/toto execute /var/www/html/toto/index.html as wanted.
Request on localhost/test_slim downloads the /var/www/html/test_slim/src/public/index.pxp
Request on localhost/test_slim/hello returns us a Nginx error page (404 if hello folder doesn't exist, 403 if it does).
Request on localhost/test_slim/src/public/ exectute the /var/www/html/test_slim/src/public/index.php file.

Configuration - Nginx with multiple sites in subfolders

I want to install my vps to develop multiple websites at the same time.
My root directory structure:
/var/www/html/web1;
/var/www/html/web2;
/var/www/html/wordpress1;
/var/www/html/wordpress1;
/var/www/html/drupal1;
/var/www/html/drupal2;
I want to link to the website http://123.123.123.123/drupal1 and 123.123.123.123 is the IP of the VPS.
Now whenever I add a website I have to edit the config file. Is there a way to install it for every page?
Here is my nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #sub_rewrite {
rewrite ^/drupal1/(.*)$ /drupal1/index.php?$1;
}
location /drupal1/ {
try_files $uri #sub_rewrite;
}
}
Thanks everyone!

multiple server block only 1 server block is loaded in mobile

I am creating server block in sites-available nginx. I have 2 server name it works fine if access to browser but in mobile it will show the i.p address and it will load the first app, I cannot load the my second app because If I dial in url mysecondsubdomain.mydomain.com it will redirect to i.p address and the first app will be serve.. I don't know why it behaves like this
updated : I noticed also in browser after few hours when I type mysecondsubdomain.mydomain.com i will be redirected to i.p adress and it will serve my first app
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/banking/public/;
index index.php index.html index.htm;
server_name banking.mydomain.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
listen [::]:80;
root /var/www/inventory/public/;
index index.php index.html index.htm;
server_name inventory.mydomain.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

nginx with symfony2 in subdirectory AND normal projects

I have a problem with my nginx configuration.
On most of pages and google search entries i found things like "nginx and symfony2" or "nginx and symfony2 in subdirectory".
But i not find some thing about this configuration:
Normal php file projects (path like localhost/project -> /srv/www/project/index.php / ruled per index)
Symfony2 projects (path like localhost/symfony2 -> /srv/www/symfony2/web/app.php (default) and manually app_dev.php / unknown!?)
Current configuration
server
{
listen 80;
listen [::]:80 default_server ipv6only=on;
root /srv/www;
index index.php index.html index.htm app.php app_dev.php;
try_files $uri $uri/ index.php #rewriteapp /index.php;
server_name localhost;
location ~ ^\/symfony2
{
#rewrite ^\/symfony2(.*)$ /symfony2/web$1 last;
#root /srv/www/symfony2/web;
alias /srv/www/symfony2/web;
}
location #rewriteapp
{
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ \.php(/|$)
{
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Thank's in advance for each tip!
This configuration should help.
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /srv/www;
index index.php index.html index.htm app.php app_dev.php;
server_name localhost;
location / {
try_files $uri $uri/ /app.php /index.php;
location ~ .*\.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
location /symfony2 {
alias /srv/www/symfony2/web;
location ~ .*\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /symfony2(?<path>.*)$ {
root /srv/www/symfony2/web;
try_files $path $path/ /app.php$path;
}
}
}

Resources