How to setup php to run from a single location in Nginx - wordpress

I've been struggling with this for some time now. I want to setup a Wordpress blog to run from a "/blogname" path on a server instead of the root. I also want the path to have a different name then the directory where the Wordpress scripts are since the server itself will run django.
I have Nginx as a reverse proxy and I set up php-fpm to run the wordpress. Here's my Nginx configuration file:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#tcp_nopush on;
#gzip on;
server {
root /Users/username/Dev/Wordpress/;
index index.php index.html index.htm;
listen 8080;
server_name localhost;
# Do not serve hidden files
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
# Static files
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
# This is the problem
location /blogname {
try_files $uri $uri/ /index.php;
rewrite /blogname(.*) /blog$1 last;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Users/username/Dev/Wordpress/blog$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
}
Right now when I visit localhost:8080/blogname I just download the index.php script instead of executing it.
Other tips are also welcome.

Replace this
location /blogname {
try_files $uri $uri/ /index.php;
rewrite /blogname(.*) /blog$1 last;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Users/username/Dev/Wordpress/blog$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
with this
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
}
I hope this should work for you because this is what I use for my local server.

Related

NGINX - Allow any location and alias

I'm looking to make Nginx allow any subfolder to redirect to a specific directory, but store the chosen subfolder in a header.
So for example I have this at the moment which works:
location ^~ /AhRnfKlM {
alias /var/www/html/admin;
index index.php index.html index.htm;
location ~ \.php$ {
limit_req zone=one burst=8;
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass php;
include fastcgi_params;
fastcgi_buffering on;
fastcgi_buffers 96 32k;
fastcgi_buffer_size 32k;
fastcgi_max_temp_file_size 0;
fastcgi_keep_conn on;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
So if I go to http://website.com/AhRnfKlM/index.php it'll work no problem, but what I want is for me to be able to pick any subfolder such as http://website.com/test123/index.php and it will still alias correctly, but store test123 as a header such as X-AuthCode, which I can read in PHP, check against a mysql database of allowed authentication codes and decide what to do from there. This way I can have specific access codes for specific admins or allow one time access codes to exist without modifying NGINX with new aliases.
I've tried various things with regex such as:
location ~ ^(/[^/]+) {
alias /var/www/html/admin;
add_header X-AuthCode $1;
index index.php index.html index.htm;
location ~ \.php$ {
limit_req zone=one burst=8;
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass php;
include fastcgi_params;
fastcgi_buffering on;
fastcgi_buffers 96 32k;
fastcgi_buffer_size 32k;
fastcgi_max_temp_file_size 0;
fastcgi_keep_conn on;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
But can't get it to work! I just get 403 forbidden.
Entire server block (after Ivan's suggestion):
server {
listen 80;
location ~ ^/(?<authcode>[^/]+) {
alias /var/www/html/admin;
index index.php index.html index.htm;
location ~ \.php$ {
limit_req zone=one burst=8;
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass php;
include fastcgi_params;
fastcgi_buffering on;
fastcgi_buffers 96 32k;
fastcgi_buffer_size 32k;
fastcgi_max_temp_file_size 0;
fastcgi_keep_conn on;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param AUTHCODE $authcode;
}
}
}
Thank you :)
With add_header directive you are adding X-AuthCode header to the nginx response to the user browser after the response was received by nginx from your PHP backend. What you should do instead is to pass your URI prefix with a fastcgi_param directive to your PHP backend, e.g.
location ~ ^/(?<authcode>[^/]+) {
...
location ~ \.php$ {
...
fastcgi_param AUTHCODE $authcode;
...
and then check the $_SERVER['AUTHCODE'] content.
But this does not answer the question why do you receive 403 HTTP error. I think there are other locations in your config that can catch a request before this location did it. Can you test only this location without any others? If your first example works, this one should work too.

How to resolve "Index of" problem on nginx

ON nginx I configured this role :
On server file:
location ~ .php$ {
root /var/www/public;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /var/www/public$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# On Domain file
location /support {
try_files $uri $uri/ /index.php;
autoindex on;
autoindex_exact_size off;
}
#for admin panel
location ^~ /support/admin {
try_files $uri $uri/ /admin/index.php;
autoindex on;
autoindex_exact_size off;
}
I solved 403 error problem when I added autoindex on; line,
but I see "index of support" message when I Click on my Domain
and when I want to Click on index.html file, this file not to show, and instead of it, download is done.
How can I resolve this problem and What is my problem in my shared code
Try this
location /support {
default_type "text/html";
types { application/octet-stream html; }
…
}

Why is Custom Error Page not working in my nginx configuration?

I have following server block:
server {
listen 80;
server_name utsav-dev.domain.org;
root /var/www/domain-utsav-dev/web;
access_log /var/log/nginx/domain-utsav-dev-access.log;
error_log /var/log/nginx/domain-utsav-dev-error.log error;
error_page 404 = /var/www/domain/web/50x.html;
set $thttps $https;
set $tscheme $scheme;
if ($http_x_forwarded_proto = http) {
set $thttps off;
set $tscheme "http";
}
if ($http_x_forwarded_proto = HTTP) {
set $thttps off;
set $tscheme "http";
}
index app.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /app.php/$1;
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 86400;
access_log off;
add_header Cache-Control "public";
}
location ~ \.php {
fastcgi_index app.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
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;
fastcgi_param APPLICATION "www";
fastcgi_param APPLICATION_ENV "dev";
fastcgi_param PDFLIBLICENSEFILE "/etc/php5/fpm/pdflib.license";
fastcgi_param HTTPS $thttps;
}
location ~ /\.ht {
deny all;
}
chunked_transfer_encoding off;
}
Now as per script if a page is not found, nginx should internally redirect to 50x.html but when I try to open http://utsav-dev.domain.org/blah.html it gives me error "File Not Found" than custom 404 page I was expecting. Why?
try to set fastcgi_intercept_errors on; in your fastcgi location.
fastcgi_intercept_errors on | off;
Determines whether FastCGI server responses with codes greater than or equal to 300 should be passed to a client or be redirected to nginx for processing with the error_page directive.
when you visit http://utsav-dev.domain.org/blah.html:
first check each location. No one hit
it go through the try_files. Hit the #rewrite location.
the uri changed to /app.php/blah.html
hit the fastcgi location.
you should set fastcgi_intercept_errors on; to let error_page work

nginx redirects all php requests to domain root folder

I've got Freebsd 8 srv with nginx, php, mysql, phpBB forum, local extjs app, bugzilla, joomla and PhpMyAdmin (/pma)
The problem is when I go to pma and login through I'm redirected to domain_name/index.php?token... instead of /domain_name/pma/index.php?token...
Same thing happes when I login to joomla admin part (domain_name/administraton/index.php. redirected to domain_name/index.php)
I understand that is a location config issue probably, but i cant find out where it is.
Config below:
user www;.
worker_processes 4;
pid /var/run/nginx.pid;
error_log /var/log/nginx-error.log warn;
events {
worker_connections 1024;
use kqueue;
}
http {
gzip on;
gzip_static on;
gzip_vary on;
gzip_http_version 1.1;
gzip_min_length 700;
gzip_comp_level 6;
gzip_disable "msie6";
include mime.types;
default_type application/octet-stream;
# log options.
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
# nginx options
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
server_tokens off;
# fastcgi
fastcgi_intercept_errors on;
# virtual server
server {
listen 80;
server_name domain_name www.domain_name;
server_name_in_redirect off;
rewrite 301 http://domain_name$request_uri;
access_log /var/log/haim_access_log main;
error_log /var/log/haim_error_log error;
root /usr/local/www;
# phpBB: Support Clean (aka Search Engine Friendly) URLs
location / {
try_files $uri $uri/ /index.php?$args;
}
# Joomla: caching of files
location ~* \.(ico|pdf|flv)$ {
expires 30d;
}
location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
expires 14d;
}
# Joomla: deny running scripts inside writable directories
location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
return 403;
error_page 403 /403_error.html;
}
# error pages 40x
error_page 404 /40x.html;
location = /nginx-distr/40x.html {
}
# phpBB 3 forum config
location /forum {
}
# phpBB 3: Deny access to internal phpbb files.
location ~ /(config\.php|common\.php|includes|cache|files|store|images/avatars/upload) {
root /usr/local/www/forum;
deny all;
# deny was ignored before 0.8.40 for connections over IPv6.
# Use internal directive to prohibit access on older versions.
# internal;
}
# phpMyAdmin
location ~ /pma4/(.*\.php)$ {
root /usr/local/www/pma4;
index index.php;
fastcgi_pass localhost:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/local/www/pma4/$1;
fastcgi_param DOCUMENT_ROOT /usr/local/www/pma4;
}
# bugzilla
location ~ ^/bugzilla/(.*\.cgi) {
fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap.sock;
fastcgi_param SCRIPT_FILENAME /usr/local/www/bugzilla/$1;
fastcgi_param DOCUMENT_ROOT /usr/local/www/bugzilla;
include fastcgi_params;
}
# php
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
# cgi
location ~ [^/]\.cgi(/|$) {
fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
# phpBB: Deny access to version control system directories.
location ~ /\.svn|/\.git {
deny all;
}
# Original: block access for .htpasswd
location ~ /\.ht {
deny all;
}
}
}
This configuration should help
location /pma4 {
root /usr/local/www;
index index.php;
location ~ .*\.php$ {
fastcgi_pass localhost:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/local/www/pma4/$1;
fastcgi_param DOCUMENT_ROOT /usr/local/www;
}
}
After days smashing my head on the keyboard I finally found the real solution for this problem and I'm sharing here as this thread still have high priority on google search.
As stated on the link : http://www.samundra.com.np/use-phpmyadmin-with-nginx-and-php7/1374
To solve the problem, you should add the following block of code to your nginx default site-available, you will access it with :
sudo nano /etc/nginx/sites-available/default
The block:
# Phpmyadmin Configurations
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_param HTTPS on; # <-- add this line
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
# Dealing with the uppercased letters
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
I hope this helps someone one day...

NGINX Config PHP Trouble

I am relatively new to NGINX, I admit (less than 24 hours), but, I got it basically configured. I'm setting up a site for a friend, and I saw an example of some code that uses subdomains like in the code below. But, in the subdomain, PHP won't work. It just asks me to download the file if I go to "subdomain.domain.tld", but, if I go to "subdomain.domain.tld/index.php", it says "No input file specified." The subdomain is phpmyadmin, by the way.
server {
listen 80;
server_name irc.physibots.info;
rewrite (.*) http://physibots.info:3989;
}
server {
listen 80;
server_name "~^([a-z]+)?.physibots.info";
root /home/virtual/physibots.info/subdomains/$1;
index index.php index.html index.html;
location / {
autoindex on;
}
location ~ \.php {
try_files $uri /error.html
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php.socket;
include fastcgi_params;
fastcgi_split_path_info ^((?U).+\.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;
}
}
#server {
# listen 443;
# server_name localhost;
#
# charset utf-8;
#
# ssl on;
# ssl_certificate
server {
listen 80;
server_name physibots.info default;
root /home/virtual/physibots.info/public_html;
index index.php index.html index.html;
location / {
autoindex on;
}
location ~ \.php {
try_files $uri /error.html
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php.socket;
include fastcgi_params;
fastcgi_split_path_info ^((?U).+\.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;
}
}
Move the try_files to your location / { } block and change it to try_files $uri $uri/ /index.php;
location / {
autoindex on;
try_files $uri $uri/ /index.php;
}
location ~ \.php {
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php.socket;
The rest looks surprisingly good for a beginner. :)
Also- make sure you're testing with curl and not a web browser or you'll constantly fight caching.

Resources