nginx change phpmyadmin folder name change error - php

i have my phpmyadmin setup as such
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
i am looking to change the folder name so that i can access phpmyadmin through /secure
location /secure {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/secure/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/secure/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
but it keeps giving me 404 not found , any help would be appretiated guys thanks

The below works and tested
location /pma/ {
alias /usr/share/phpmyadmin/;
}
location ~ ^/pma/(.+\.php)$ {
alias /usr/share/phpmyadmin/$1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
# From fastcgi_params
include fastcgi_params;
fastcgi_param DOCUMENT_ROOT /usr/share/phpmyadmin;
}
The key is to set the below
fastcgi_param DOCUMENT_ROOT /usr/share/phpmyadmin;
this is a variable that gets se in fastcgi_params but when to below it works like a charm
change the 'pma' in both places to anything to u want and it'll work...no need for sym link
cheers

Something like this should work.
location /secure/ {
alias /usr/share/phpmyadmin/;
location ~ ^/secure/(.+\.php)$ {
alias /usr/share/phpmyadmin/$1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

Try this:
location /secure {
alias /usr/share/phpmyadmin;
index index.php index.html index.htm;
location ~ ^/secure/(.+\.php)$ {
alias /usr/share/phpmyadmin/$1;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin/$1;
fastcgi_pass php;
}
location ~* ^/secure/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
alias /usr/share/phpmyadmin/$1;
}
}

You need to use alias in this situation.
location /secure/ {
alias /usr/share/phpmyadmin/;
With above settings all the requests to /secure/ will be dropped to /usr/share/phpmyadmin/.

Related

Nginx PHP-FPM alias path

I'm really giving up on this. I have nginx+PHP-FPM, just trying to alias a path.
Desired:
https://example.com/api/v1.0/ -> /my/folder/v1.0/
I've tried alias:
server {
server_name mysite.com;
index index.php;
location /api/v1.0/ {
index index.php;
alias /my/folder/v1.0/;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
Nope. The logs reports that nginx is trying to access:
/etc/nginx/html/api/v1.0
So i changed the root:
server {
server_name mysite.com;
root /my/folder/v1.0/;
index index.php;
location /api/v1.0/ {
index index.php;
alias /my/folder/v1.0/;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
Nope. Now the alias is ignored and nginx trying to access:
/my/folder/v1.0/api/v1.0
I'm out of ideas, could you please help me?
Ok I was able to reproduce and fix it locally.
Here's what I ended up with:
# where the actual API is
root /1int/code/test/nginx;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# This basically drops /api/1.0 prefix from the URL
location ^~ /api/1.0 {
rewrite ^/api/1.0/(.*)$ /$1 last;
}
With this config, when I access /api/1.0/api.php it fetches <root>/api.php

Nginx change the root location to wordpress site

Currently I have a Wordpress site under /var/www/html/wordpress and other php projects under /var/www/html/projects. I want my root location to point to wordpress one. Here is my current nginx config:
server {
listen 80 default_server;
server_name _;
set $yii_bootstrap "index.php";
root /var/www/html;
client_max_body_size 2M;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /projects/inspection/ {
root /var/www/html;
try_files $uri /index.php$is_args$args;
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;
fastcgi_pass 127.0.0.1:9000;
}
location /projects/ {
root /var/www/html;
index index.html $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args /index.php$is_args$args;
}
location / {
root /var/www/html/wordpress;
try_files $uri $uri/ =404;
index index.php index.html index.htm;
}
location ~ \.php{
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass php-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
}
But it returns 404. I tried to put alias instead and it returns 403 forbidden. How should I handle this?

nginx $document_root with subdirectories insert the subdirectory in its path

I'm working with subdirectories. I want "babylon/webmail" to go to my rainloop webmail client.
location ^~ /webmail {
root /srv/rainloop/public_html;
try_files $uri $uri/ /webmail/index.php?$query_string;
access_log /srv/rainloop/logs/access.log;
error_log /srv/rainloop/logs/error.log;
index index.php;
access_log /var/log/nginx/scripts.log scripts;
location ~ \.php$ {
#fastcgi_index index.php;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
#fastcgi_keep_conn on;
#include /etc/nginx/fastcgi_params;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /srv/rainloop/public_html/index.php;
#include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ^~ /webmail/data {
deny all;
}
}
However this
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
doesn't work at all. It prints out: /srv/rainloop/public_html/webmail/index.php; That file doesn't exist in the directory structure, but: /srv/rainloop/public_html/index.php
fastcgi_param SCRIPT_FILENAME /srv/rainloop/public_html/index.php;
P.S.: After hardcoding, I don't get any error at all, but the page is blank with some rainloop code source code.
The path to the file is calculated by concatenating the value of root to the URI. The URI contains /webmail/index.php, otherwise it would not match the location block.
You probably mean to use alias instead of root as that directive removes the value of the prefix location when calculating the path to the file. See this document for details.
location ^~ /webmail {
alias /srv/rainloop/public_html;
if (!-e $request_filename) { rewrite ^ /webmail/index.php last; }
...
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Avoid using try_files and alias in the same block, due to this long term issue, and see this caution on the use of if. Use $request_filename for the SCRIPT_FILENAME value.

nginx - codeigniter 404 error for all urls

I have uploaded my files(Codeigniter Framework) in abcd directory ( url : example.com/abcd ) . All the links inside give 404 error . For removing index.php, I have placed this nginx.conf file in my root directory ( on server : /.../abcd/)
Here is the code for the nginx.conf file :
server {
server_name example.com/abcd;
root /..../abcd;
index index.html index.php index.htm;
# 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 exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass example.com/abcd;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
In apache, it was easy using rewrite, i am now stuck with nginx. Please help, I am using nginx for the first time
Try below code.. taken from
https://serverfault.com/questions/695521/codeigniter-in-subdirectory-on-nginx-404
location /abcde/ {
alias /var/www/abcde/;
try_files $uri $uri/ /abcde/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass backend;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

nginx change root on specific url

I have one nginx http server configuration like this:
server {
listen 80;
server_name test.loc;
root /var/www/test;
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.+)$;
set $fsn /index.php;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fsn;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fsn;
fastcgi_read_timeout 180;
}
location / {
index index.html index.php;
try_files $uri $uri/ /index.php?$args;
}
location /sample/ {
root /var/www/test2/;
}
}
I want to address http://test.loc/sample root was in the folder /var/www/test2 but no matter how I tried, displays the contents of /var/www/test.
Thank you in advance.
UPD:
now i have this config:
server {
listen 80;
server_name test.loc;
root /var/www/test;
error_log /var/log/nginx/mytest.log;
index index.html index.php;
location / {
index index.html index.php;
try_files $uri $uri/ /index.php?$args;
set $root /var/www/test;
}
location /sample {
index index.html index.php;
try_files $uri $uri/ /index.php?$args;
alias /var/www/test2;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
location ~ \.php$ {
#try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_split_path_info ^(.+\.php)(.+)$;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
all works fine, but when i get content of this url http://test.loc/sample/ i see content /var/www/test2/sample/index.php.
I would like to be displayed /var/www/test2/index.php
Any ideas? Thanks.

Categories