How use htacces to run index.php in subfolder in nginx? - php

I'm new in nginx.
I stack on run my website using nginx.
I have try to convert htaccess to use nginx.conf or default.d/*.conf but my site still is not working.
Here are my files:
--- .htaccess ---
|-- public |
| --- index.php
| --- .htaccess
|-- application
first htaccess
RewriteEngine on
RewriteRule ^(.*) public/$1 [L]
second htaccess same folder with index.php
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
This my nginx.conf after edit:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
autoindex off;
location / {
# first htaccess configuration
rewrite .* /public/index.php last;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
And the default.d/*.conf
index index.php index.html index.htm;
# htaccess configuration
autoindex off;
if(!-e $request_filename){
rewrite^(.+)$ /index.php?url=$1 break;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}
Can anyone help me to run this in nginx?

One option is to set .../public as the document root, and rewrite any URI beginning with /public/ to /:
root /usr/share/nginx/html/public;
location / {
try_files $uri #index;
}
location #index {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
location /public/ {
rewrite ^/public(.*)$ $1 last;
}
location ~* \.php$ {
try_files $uri =404;
...
}
However, if you prefer to use your current document root, this may suit your requirements:
root /usr/share/nginx/html;
location / {
try_files $uri /public$uri #index;
}
location #index {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
location ~* \.php$ {
try_files $uri /public$uri =404;
...
}

Related

Site is not executing PHP code when Migrated from Apache to Nginx server

Recently I migrated the site from the Apache server to the Nginx server, On this Nginx server there is already another PHP site is hosted, but only the PHP code of this migrated site is not executing.
Below is a conf file for this site. When I try to run the site on the browser only Html code is displaying and PHP code is not executing.
server {
listen 80;
server_name xyzorg.in;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name xyzorg.in;
ssl on;
ssl_certificate /etc/pki/tls/certs/xyzorg_in.pem;
ssl_certificate_key /etc/pki/tls/certs/xyzorg_in.key;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.1 TLSv1.2; # Dropping
root /usr/local/www/site;
index index.html index.php report.html;
# redirect server error pages to the static page /50x.html
location / {
try_files $uri $uri?$args $uri/ /index.php?$uri&$args /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location = /favicon.ico { log_not_found off; access_log off;}
location = /robots.txt { log_not_found off; access_log off;}
location ~ /.well-known { allow all; }
}
In the apache server, there was a .htaccess file with the below code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^xyzorg.in [NC]
RewriteRule ^(.*)$ http://www.xyzorg.in/$1 [L,R=301]
RewriteCond %{REQUEST_URI} index\.html
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]

Problem with switching PHP code from Apache to Nginx

On Apache we used to have a URL like MyUrl.com/UserId and in the root index.php code executed that got the request_uri that had the UserId. In Nginx instead it looks for a folder called UserId which doesn’t exist. How can I modify the Nginx config to still use the root index.php file instead of look for a non existent folder?
Here is my current nginx config:
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/before/*;
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name MyUrl.com;
root /home/forge/MyUrl.com;
rewrite_log on;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/MyUrl.com/766650/server.crt;
ssl_certificate_key /etc/nginx/ssl/MyUrl.com/766650/server.key;
ssl_protocols TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /var/log/nginx/MyUrl.com-access.log combined;
error_log /var/log/nginx/MyUrl.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 180;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
location /api/v1/ {
rewrite_log on;
index index.php;
fastcgi_index index.php;
error_page 405 =200 $uri;
if (!-e $request_filename){
rewrite /api/v1/(.*)$ /api/v1/api.php?request=$1 break;
}
}
client_max_body_size 128M;
}
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/after/*;
Apache and Nginx use different configuration formats. You need to convert what is currently in your .htaccess file into a format that Nginx understands.
This is an example of an Apache .htaccess configuration:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
In Nginx it could look something like this:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Take a look at these questions for more details:
Converting .htaccess to nginx (mod_rewrite)
Convert htaccess to nginx rewrite
Also this tool might help with the conversion:
https://winginx.com/en/htaccess

NGINX is serving me php files instead of executing them

I've found many people who had the same problem as me, however I couldn't find the right solution.
I'm running a NGINX server via vagrant and homestead. On the production end I'm using apache and therefore I have an htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ cms-system/public/$1 [L]
I use this htaccess to rewrite all urls (except cms-system/public and assets) to my index.php which is in cms-system/public.
I've tried to convert this htaccess to a nginx config with this tool: https://winginx.com/en/htaccess Which did not work very well.. I've made some adjustments. The exception rules work, however I can't get the rewrite for the index.php to work. Could someone please help me?
server {
listen 80;
listen 443 ssl http2;
server_name company.dev;
root "/home/vagrant/company/";
index index.html index.htm index.php;
charset utf-8;
location ~ ^/cms-system/public/(.*) {
}
location ~ ^/assets/(.*) {
}
location / {
if ($request_uri !~ "-f"){
rewrite ^(.*)$ /cms-system/public/$1 break;
}
}
# original location rule
# location / {
# try_files $uri $uri/ /index.php?$query_string;
# }
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/company.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/company.dev.crt;
ssl_certificate_key /etc/nginx/ssl/company.dev.key;
}
This is all you need:
location / {
try_files $uri #rewrite;
}
location #rewrite {
if ($uri !~* "^/(cms-system\/public|assets)$") {set $block "A";}
if (!-e $request_filename) {set $block "${block}B";}
if ($block = "AB") {rewrite ^(.*)$ /cms-system/public/$1 last;}
}
instead of:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ cms-system/public/$1 [L]
This is recommended for security reasons:
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
deny all;
}
Read also:
porting-standard-apaches-mod_rewrite-rules-to-nginx
How do I convert mod_rewrite (QSA option) to Nginx equivalent?

Nginx + Codeigniter rewrite

after i move my app to Nginx server things just blow up.
My site is loaded, but when i try to access some controller return 404 not found.
Read alot of articles how to configure nginx.conf but without success.
Here is my nginx.conf
server {
listen 80;
server_name example.com;
root /var/www/html/travel;
index index.php;
# Enable rewrite error log
error_log /var/log/nginx/travel.error_log debug;
rewrite_log on;
# Any HTTP request other than those for assets folder, files folder and robots.txt
# is treated as a request for your index.php file.
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~* ^/(assets|files|robots\.txt) { }
# Deny access to .htaccess files, if Apache's document root
# concurs with Nginx's one
location ~ /\.ht {
deny all;
}
}
Strange thing is that in error.log has no errors.
Here is mine Codeigniter config.php
$config['base_url'] = 'http://example.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Check if this helps. Replace both occurrences of <source_directory>; with your correct value:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root <source_directory>;
rewrite_log on;
index index.html index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.htaccess
{
deny all;
}
error_page 404 /404.html;
location = /40x.html
{
}
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
}
}
server {
server_name domain.tld;
root /var/www/html/travel;
index index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
This is my config with some changes.

Phalcon + Nginx Routes / Rewrites not working

I finally got Phalcon+phpfpm+nginx on a Osx to work... trying to setup a quick REST application I am stuck, I believe with an nginx rewrite.
I am always getting a Route not matched error.
File Structure:
/myapp/
public/
index.php
app/
models/
...
index.php
<?php
$app = new \Phalcon\Mvc\Micro();
$app->get('/api/robots', function() {
echo json_encode(['true']);
});
$app->notFound(function(){
echo json_encode(['Route Not Found']);
});
$app->handle();
nginx config file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
root /var/www/bmex/public;
listen 80;
server_name some.name.com;
charset utf-8;
#access_log /var/log/nginx/host.access.log main;
set $root_path '/var/www/bmex/public';
location / {
root $root_path;
index index.php index.html index.htm;
# if file exists return it right away
if (-f $request_filename) {
break;
}
# otherwise rewrite it
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?_url=/$1 last;
break;
}
}
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 ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
}
}
It appears the Phalcon docs are wrong... or I am missing something... I will find out soon enough.
Replace _uri=/$1 for =$1 ... so far it works..
rewrite ^(.+)$ /index.php?_url=/$1 last;
rewrite ^(.+)$ /index.php?_url=$1 last;

Categories