How to configure multiple sites with same URL on Nginx - 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.

Related

Multiple Domain in single VPS using VHOST

I want to create multiple sites in one vps, example sites on port 80 use www.domain1.com and on port 8080 use www.domain2.com. can nginx or anything handle that?
i have tried to use port forwarding, but is not working well
my nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name domain1.com;
root /var/www/html;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# PHP
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
and my virtualhost.conf
#Virual Host
server {
listen 8080;
access_log /var/log/nginx/nginx-access.log;
error_log /var/log/nginx/nginx-error.log;
# Root
root /var/www/web2/public;
index index.php index.html index.htm;
# server name
server_name domain2.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
if possible, how the way?
You can run both domains on port 80. you don't need to change port to 8080.
In nginx.conf change server_name from domain1.com back to default_server;
In your /etc/nginx/conf.d/ directory create two config files, domain1-com.conf and domain2-com.conf
donain1-com.conf
server {
listen 80;
server_name domain1.com www.domain1.com;
access_log /var/log/nginx/domain1-com-access.log;
error_log /var/log/nginx/domain1-com-error.log;
root /var/www/web1/public;
index index.php index.html index.htm;
# PHP
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
domain2-com.conf
server {
listen 80;
server_name domain2.com www.domain2.com;
access_log /var/log/nginx/domain2-com-access.log;
error_log /var/log/nginx/domain2-com-error.log;
root /var/www/web2/public;
index index.php index.html index.htm;
# PHP
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Slim 4 Error 404 not found on nginx web server

Please help me, I want to move the project rest api (Slim 4 use Design Pattern) from apache to nginx, but I haven't found a solution to this problem, how to convert .htaccess to nginx server configuration, the error I found is always 404 (Slim / Middleware / RoutingMiddleware.php)
thank you for your help
this server config nginx
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.php index.htm index.nginx-debian.html;
autoindex off;
server_name _;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
}
location /FjbBaseApi {
index index.php index.html index.htm;
try_files $uri $uri/ /FjbBaseApi/public/index.php;
rewrite ^/$ /public/ break;
rewrite ^(.*)$ /public/$1 break;
}
location ~ /\.ht {
deny all;
}
}

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;
}
}

Vhost on nginx does not function as expected

My domain registrar's DNS hits my server and gets the nginx default page, so that's properly configured
I copied an nginx vhost that is currently working, changed the server_name and the name of the conf file and nothing else.
Diff:
diff 701sm.club.conf drz400.info.conf
3c3
< server_name www.701sm.club 701sm.club;
---
> server_name www.drz400.info drz400.info;
then I restarted nginx.
Here is the entire non-functioning vhost:
server {
listen 80;
server_name www.701sm.club 701sm.club;
index index.php index.html index.htm;
access_log /var/www/drz400.info/logs/access.log;
error_log /var/www/drz400.info/logs/error.log;
location / {
root /var/www/drz400.info/production;
}
location ~ \.php$ {
root /var/www/drz400.info/production;
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Both sites should hit my .../production/index.html, but only one does. What could be wrong here?
Please try the following code,
server {
listen 80;
server_name www.701sm.club 701sm.club;
index index.php index.html index.htm;
access_log /var/www/drz400.info/logs/access.log;
error_log /var/www/drz400.info/logs/error.log;
root /var/www/drz400.info/production;
location / {
try_files $uri /index.html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Just got curious about your log directive, you point out the log to /var/www/drz400.info/logs/error.log while your domain was www.701sm.club. If you access www.701sm.club, is there any error on your error.log ?
And what type of error was occurred ?

Why doesn't my 301 redirect work in nginx?

I have the following server block and I'm trying to do a 301 redirect so www.realestatelicensebystate.com goes to http://realestatelicensebystate.com for SEO purposes. Problem is, after I put the line in there, I'm getting the generic "Welcome to nginx" screen. Here is the code:
server {
listen 80;
server_name www.realestatelicensebystate.com;
rewrite ^/(.*)$ http://realestatelicensebystate.com/$1 permanent;
access_log /srv/www/realestatelicensebystate.com/logs/access.log;
error_log /srv/www/realestatelicensebystate.com/logs/error.log;
location / {
root /srv/www/realestatelicensebystate.com/public_html;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/realestatelicensebystate.com/public_html$fastcgi_script_name;
}
}
Anything stand out or is there anything I should be doing better? I'm brand new to nginx and learning.
The reasons why you are getting default nginx page is here:
server_name www.realestatelicensebystate.com;
rewrite ^/(.*)$ http://realestatelicensebystate.com/$1 permanent;
Your server is listening for name www.realestatelicensebystate.com and you are redirecting to realestatelicensebystate.com.
You need to create either another vhost config or new server block for your new name.
server {
listen 80;
server_name www.realestatelicensebystate.com;
rewrite ^ http://realestatelicensebystate.com$request_uri? permanent;
}
server {
listen 80;
server_name realestatelicensebystate.com;
access_log /srv/www/realestatelicensebystate.com/logs/access.log;
error_log /srv/www/realestatelicensebystate.com/logs/error.log;
location / {
root /srv/www/realestatelicensebystate.com/public_html;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/realestatelicensebystate.com/public_html$fastcgi_script_name;
}
}

Resources