Slim 4 Error 404 not found on nginx web server - nginx

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

Related

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.

phpMyAdmin 404 error on Digital Ocean hosting

I'm getting a 404 error after install phpMyAdmin using Digital Ocean's guide. I have multiple domains setup on Ubuntu running nginx. There is a phpmyadmin directory within /var/www/. The only difference from the guide was the following command:
sudo ln -s /usr/share/phpmyadmin/ /var/www
Do I need to add server block possibly? Since I have multiple domains, each of them has a separate configuration file.
Sample config file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/domain1.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name MY_IP_ADDRESS;
location / {
try_files $uri $uri/ =404;
}
location /phpmyadmin {
root /var/www/;
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
try_files $uri /index.php;
}
location ~ /\.ht {
deny all;
}
}
I'm not great at server administration. Any suggestions?
Define this server block it worked for me.
A basic configuration for phpmyadmin in you case.As you have created a symbolic link in /var/www.
sudo ln -s /usr/share/phpmyadmin/ /var/www
server {
listen 80;
server_name website.in www.website.in;
autoindex on;
location /phpmyadmin {
root /var/www/;
index index.php index.html;
location ~ \.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;
try_files $uri $uri/ /index.php;
}
}
location / {
root /var/www/your/path;
index index.php index.html;
location ~ \.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;
try_files $uri $uri/ /index.php;
}
}
}

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

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

Configuring subdomain in NGINX

I am trying to configure a subdomain in NGINX. Where am I going wrong?
Following is the configuration file:
server {
listen 80;
server_name www.teamomattic.com;
rewrite ^/(.*) http://teamomattic.com permanent;
}
server {
listen 80 default;
server_name teamomattic.com *.teamomattic.com;
root /home/jclark/web/teamomattic.com;
access_log /var/log/nginx/$host-access.log;
error_log /var/log/nginx/dev-error.log error;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php/$1;
}
location ~ \.php {
# try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name test.teamomattic.com;
root /home/jclark/web/teamomattic.com/images;
access_log /var/log/nginx/$host-access.log;
error_log /var/log/nginx/dev-error.log error;
index index.php index.html index.htm;
}
Just guessing. I would do it this way.
server
{
listen 80;
server_name subdomain.teamomattic.com;
location / { return 303 http://teamomattic.com$request_uri; }
}
303 is the new temporary redirect. I never use permanent redirects, b/c you stay flexible and don't need to ask your custormers to clear cache.
You may not need this location block wrapper and can directly use return in server. But it is best practice to use always location, b/c you can add more locations easily.
Please use https if possible.
request_uri passes path and query string through - so you don't loose that info.

Resources