This question already has answers here:
Nginx serves .php files as downloads, instead of executing them
(28 answers)
Closed 3 years ago.
I have nginx, php5.6-fpm, setup on the default port (9000).
And I have gone through several forums, SO pages and documentation to alleviate this problem, but to no avail.
The index.php file of my web-app executes as should, but all other .php files get downloaded, It appears it has to do with my rewrite-rules and not Fast Process Manager per-say, but I ca't seem to figure.
Below is my site.conf
server {
listen 80 default_server;
listen [::]:80 ipv6only=on;
server_name xx.xx.xx.xx;
root /var/www/foo.bar.com/html;
# index.php
index index.php index.html;
error_page 404 /404.php;
autoindex off;
location / {
rewrite ^/(.*)/p/(.*)?$ /product.php?slug=$2 break;
rewrite ^/?([A-Za-z0-9_-]+)/?$ /vendor.php?vendor=$1 break;
if (!-e $request_filename){
rewrite ^(.*)$ /$1.php break;
}
}
location /cart {
rewrite ^/cart/?$ /cart.php break;
}
location /checkout {
rewrite ^/checkout/?$ /checkout.php break;
}
location /search/ {
rewrite ^/search/?$ /search.php break;
rewrite ^/search/(.*)?$ /search.php?slug=$1 break;
rewrite ^/search/brand/(.*)?$ /search.php?brandslug=$2 break;
}
location /brand {
rewrite ^/brand/(.*)?$ /search.php?brand=$1 break;
}
location /brands {
rewrite ^/brands/?$ /productbrands.php break;
}
location /404 {
rewrite ^/404/?$ /404.php break;
}
location /vendors {
rewrite ^/vendors/?$ /vendors.php break;
}
include /etc/nginx/mime.types;
# handle .php
location ~* \.php(/|$) {
try_files $uri =404;
include /etc/nginx/mime.types;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param HTTPS off;
include nginxconfig.io/php_fastcgi.conf;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
include /etc/nginx/mime.types;
access_log off;
log_not_found off;
expires 360d;
}
include nginxconfig.io/general.conf;
}
Please add this:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
}
Related
tried every config shared in stackoverflow but not working
used this one
location /blog/ {
index index.php;
try_files $uri $uri/ /blog/index.php?$args;
}
but instead of page opening every .php file under /blog downloading in the browser :( here is my current Nginx config please help currently I am using yetishare file sharing script in the main domain but want to use WordPress /blog in sub-directory
current Nginx config
server {
listen ***:443 ssl http2;
server_name devdrive.cloud www.devdrive.cloud;
root /home/admin/web/devdrive.cloud/public_html;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/devdrive.cloud.log combined;
access_log /var/log/nginx/domains/devdrive.cloud.bytes bytes;
error_log /var/log/nginx/domains/devdrive.cloud.error.log error;
ssl_certificate /home/admin/conf/web/devdrive.cloud/ssl/devdrive.cloud.pem;
ssl_certificate_key /home/admin/conf/web/devdrive.cloud/ssl/devdrive.cloud.key;
ssl_stapling on;
ssl_stapling_verify on;
include /home/admin/conf/web/devdrive.cloud/nginx.hsts.conf*;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\.(?!well-known\/) {
deny all;
return 404;
}
location / {
try_files $uri $uri/ /index.php?$args;
location ~* ^.+\.(ogg|ogv|svg|svgz|swf|eot|otf|woff|woff2|mov|mp3|mp4|webm|flv|ttf|rss|atom|jpg|jpeg|gif|png|webp|ico|bmp|mid|midi|wav|rtf|css|js|jar)$ {
expires 30d;
fastcgi_hide_header "Set-Cookie";
}
location ~* /(?:uploads|files)/.*.php$ {
deny all;
return 404;
}
location ~ [^/]\.php(/|$) {
if (!-e $request_filename) { rewrite ^/(.*) /index.php?_page_url=$1 last; }
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
fastcgi_pass unix:/run/php/php8.0-fpm-devdrive.cloud.sock;
fastcgi_index index.php;
include fastcgi_params;
include /etc/nginx/fastcgi_params;
fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED on;
include /home/admin/conf/web/devdrive.cloud/nginx.fastcgi_cache.conf*;
if ($request_uri ~* "/wp-admin/|wp-.*.php|xmlrpc.php|index.php|/store.*|/cart.*|/my-account.*|/checkout.*") {
set $no_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|woocommerce_items_in_cart|woocommerce_cart_hash|PHPSESSID") {
set $no_cache 1;
}
}
}
#location / {
if (!-e $request_filename) {
rewrite ^/(.*) /index.php?_page_url=$1 last;
}
#}
location /files/ {
internal;
}
# these locations would be hidden by .htaccess normally
location /logs/ {
deny all;
}
location /error/ {
alias /home/admin/web/devdrive.cloud/document_errors/;
}
location /vstats/ {
alias /home/admin/web/devdrive.cloud/stats/;
include /home/admin/web/devdrive.cloud/stats/auth.conf*;
}
proxy_hide_header Upgrade;
include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /home/admin/conf/web/devdrive.cloud/nginx.ssl.conf_*;
}
I have php website (symfony framework running in dev env) under example.com and wordpress blog under example.com/blog/ .
Now I'm trying make make wordpress working under this set up.
As of right now I have only static files served by nginx.
I can't make php files executed.
Symfony app root dir: /data/example.com
Wordpress root dir: /data/example.com-blog/web
My nginx config
server {
listen 80;
server_name example.com;
charset utf-8;
access_log /var/log/nginx/example.com_access.log main;
error_log /var/log/nginx/example.com_error.log;
root /data/example.com/web/;
index app_dev.php;
error_page 400 401 404 500 = /index.php;
location ~ /\. {
deny all;
}
location ^~ /blog {
log_not_found on;
access_log on;
root /data/example.com-blog/web;
rewrite ^/blog/([^.]+\.[^.]+)$ /$1 break;
try_files $uri $uri/ /blog/index.php$is_args$args;
location ~ \.php {
return 401; # for
}
}
location / {
try_files $uri /app_dev.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors off;
fastcgi_pass unix:/tmp/php-fpm.socket;
fastcgi_index index.php;
fastcgi_param HTTPS off;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|ico|js|css|htm|html|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|swf|flv|htc|xsl|eot|woff|ttf|svg)$ {
expires 1w;
tcp_nodelay off;
}
}
I tried to use RichardSmith's example and now I'm getting 403 error
location ^~ /blog {
alias /data/example.com-blog/web;
if (!-e $request_filename) { rewrite ^ /blog/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/tmp/php-fpm.socket;
}
}
I have configured my nginx to get data from s3 in case of 502 error thrown by php, but it is accessing data for all requests even when there is no 502 error.
Here is my configuration file,
upstream php {
server unix:/var/run/php5-fpm.sock;
}
server {
listen *:80;
server_name x.x.x.x;
root /data/www/public/;
# access_log /var/log/nginx/www.example.com.access.log main;
error_log /var/log/nginx/wf_err.log;
add_header Access-Control-Allow-Origin http://x.x.x.x;
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
index index.php;
try_files $uri $uri/ /index.php;
}
location #static{
add_header Access-Control-Allow-Origin *;
rewrite ^ $request_uri;
rewrite ^/assets/(.*)/(.*)\.css.* /assets/$1/$2.css break;
rewrite ^/assets/(.*)/(.*)\.js.* /assets/$1/$2.js break;
rewrite ^/assets/image.*/(.*)\.(.*).* /assets/image/$1.$2 break;
rewrite ^/assets/fonts/(.*)\.(.*).* /assets/fonts/$1.$2 break;
rewrite /story/([0-9]+)/([0-9]+)/.* /story/$1/$1_$2.html break;
rewrite /story/([0-9]+)/.* /story/$1/$1_1.html break;
proxy_hide_header x-amz-id-2;
proxy_hide_header x-amz-request-id;
proxy_pass http://bucket.s3.amazonaws.com;
}
location ~ \.php {
try_files $uri =404;
fastcgi_pass php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
error_page 502 504 503 #static;
fastcgi_intercept_errors on;
# return 502;
}
# deny access to .htaccess files
location ~ /\.ht {
deny all;
}
}
I have used fastcgi_intercept_errors to catch errors sent by php.
Then, using error_page directive, I change the location to #static, where the requests are processed from s3.
But I can't understand why all requests are going to s3 even when there is no error.
Thanks.
I've installed nginx and I try to run wordpress on it.
Everything works fine, except for permalinks.
Here is the vhost-file I'm using:
server {
listen 123456:80;
server_name my-domain.com;
if ($host ~* www\.(.*)) {
set $wwwless $1;
rewrite ^(.*)$ $scheme://$wwwless$1 permanent;
}
root /var/www/my-folder;
index index.php;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
(I've replaced critical data in the above code with my-domain.com, my-folder and the ip 123456.)
index.php, the admin-panel and using the standard links (.../?p=123) work finde. If I enable some of the permalinks, index.php and the admin-panel still work. But if I try to open another site of the wordpress blog, my browser downloads the index.php :(
Try something like this for the .php location config:
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Here's my wordpress config modified to suit you.
server { # the redirecting from www to non-www
server_name www.example.com;
return 301 $scheme://example.com$request_uri$is_args$query_string;
}
server {
listen 80;
server_name example.com;
root /path/to/root;
# make sure the directory /var/log/nginx exists
access_log /var/log/nginx/wordpress_access.log;
error_log /var/log/nginx/wordpress_error.log;
index index.php;
location / {
try_files $uri /index.php$request_uri$is_args$query_string;
}
location ~ \.php {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht { # avoid downloading htaccess, htpasswd, etc files
deny all;
}
}
you can try this vhost file
server {
listen 80;
server_name www.example.com example.com;
root /var/www/www.example.com/web;
if ($http_host != "www.example.com") {
rewrite ^ http://www.example.com$request_uri permanent;
}
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I'd like to be able to use
www.example.com/profiles/1234567890
instead of
www.example.com/profiles?id=1234567890
but was unable to figure out how to do so.
I found an apache equivalent here: Turn text after slashes into variables with HTACCESS but I don't know how to get it to work in Nginx. I only need the one variable which is id.
Additionally, is it possible to do a rewrite such that
www.example.com/id/1234567890
points to
www.example.com/profiles/1234567890
but the url doesn't change?
Nginx config
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
listen 80;
server_name domain.com;
root /var/www/domain.com/public;
index index.php;
access_log /var/www/domain.com/access.log;
error_log /var/www/domain.com/error.log;
rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
#rewrite ^(.+)$ /index.php?q=domain.com last;
rewrite ^(.*)$ $1.php last;
}
# catch all
error_page 404 /index.php;
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
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;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ #extensionless-php?$args;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# use fastcgi for all php files
location ~ \.php$
{
try_files $uri =404;
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;
# Some default config
fastcgi_connect_timeout 20;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
}
}
Inside the server block of your configuration file, try adding this:
rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;
Restart nginx. It should work.
More information can be found here: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
I think you put the rules swapped, try something like
rewrite ^/(?:profiles|id)/(.*) /profiles?id=$1 last;
For anyone looking for the answer, this worked:
rewrite ^/profiles/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;
rewrite ^/id/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;