Nginx downloading php content instead of serving - php

I have a ubuntu server at digitalocean where I am trying to get php to work.
I have successfully created a subdomain with nginx, though nginx keeps downloading the php files, instead of serving them. This is my default:
server {
listen 80;
listen [::]:80;
index index.html index.php;
server_name _;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I have tried to add following to the default nginx file, with no luck:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
in completion:
server {
listen 80;
listen [::]:80;
index index.html index.php;
server_name _;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
}
I am starting to think that "location / {" is targeting every content, or is there a misconfiguration somewhere? It works perfectly fine if I add html files to the subdomain, then Nginx serves them correctly, but not php.
My conf file for nginx looks like this:
server {
listen 80;
index index.php index.html;
server_name sub.example.com;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /home/simonduun/sub;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/wp-json/ {
rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
}
}

Related

Laravel returning 404 on all route configured with NginX

I have been trying to resolve this for like a day now. I have checked out all similar questions and done the same thing but this does not seem to work.
I am currently running a node JS app on the server and now I intend to add laravel to it. But all routes return the laravel 404 error page.
Here is the config file.
server {
listen 80;
listen [::]:80 ipv6only=on default_server;
root /var/www/html/dlabs.cloud-File-Service/public;
index index.html index.htm index.php;
server_name dlabs.com.ng;
location /auth/ {
proxy_pass http://localhost:3000;
proxy_pass_header Server;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /file-service/ {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location = /favicon.ico {
access_log off; log_not_found off;
}
location = /robots.txt {
access_log off; log_not_found off;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires 5d;
}
# deny access to . files, for security
#
location ~ /\. {
log_not_found off;
deny all;
}
}
PHP version is Version 7.3.
The /auth/ works perfectly.
add try_files $uri $uri/ /index.php$is_args$args;
to nginx.conf
in => /etc/nginx/nginx.conf

How to configure Nginx to return a CSV as response?

I have a test.csv file locate at the root of my VM running on Ubuntu.
I am using Nginx in Laravel 4.2.
VM IP : 1.1.1.1
Domain : www.site.com
How do I make that CSV public accessible via my site: www.site.com/csv ?
nginx
server {
listen 80 default_server;
server_name site.com www.site.com;
root /home/forge/john/public;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location /resume {
#auth_basic "Restricted";
#auth_basic_user_file /home/forge/john/.htpasswd;
try_files $uri $uri/ /index.php?$query_string;
}
location /getIconBasedOnDevicesName {
proxy_pass http://localhost:3030;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
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/default-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
If the file is located at /home/forge/john/public/test.csv, you could use:
location = /csv {
try_files /test.csv =404;
}

Nginx php5.6 fpm show blank page

I want to run node along with php on domain.com/api. My nginx configuration is as below
server {
listen 80;
server_name domain.com;
return 301 http://domain.com$request_uri;
}
server {
listen 80;
server_name domain.com;
location / {
proxy_pass http://domain.com:8080;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* \.(html|css|jpg|gif|ico|js)$ {
proxy_cache cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 301 302 30m;
expires 30m;
proxy_pass http://domain.com:8080;
}
}
location ^~ /api {
alias /var/www/html/testphp/api;
try_files $uri $uri/ #api;
location ~* \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
}
}
location #api {
rewrite ^/api/(.*)$ /api/index.php/$1 last;
}
}
But when I run file domain.com/api/test.php it gives blank page? How to solve this
I got this configuration working fully. I am running code-igniter, its working correctly.
server {
listen 80;
server_name www.domain.com;
root /var/www/html/testphp/api;
location / {
proxy_pass http://www.domain.com:8080;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* \.(html|css|jpg|gif|ico|js)$ {
proxy_cache cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 301 302 30m;
expires 30m;
proxy_pass http://www.domain.com:8080;
}
}
location /api/ {
alias /var/www/html/testphp/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}

nginx configuration to run node as domain.com and php in subfolder as domain.com/phptest

I am able to run node on port 8080 with pointing it to domain.com but I want to run php alongside of it.
Sample configuration is as below:
proxy_buffering on;
proxy_buffer_size 1k;
proxy_buffers 24 4k;
proxy_busy_buffers_size 8k;
proxy_max_temp_file_size 2048m;
proxy_temp_file_write_size 32k;
server {
listen 80;
server_name domain.com;
location / {
proxy_pass http://domain.com:8080;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* \.(html|css|jpg|gif|ico|js)$ {
proxy_cache cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 301 302 30m;
expires 30m;
proxy_pass http://domain.com:8080;
}
}
}
Now I want to run domain.com/php as php server so any request which has domain.com/php will process php and others will work on node. Is it possible?
server {
listen 80;
server_name www.domain.com;
root /var/www/html/testphp/api;
location / {
proxy_pass http://www.domain.com:8080;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* \.(html|css|jpg|gif|ico|js)$ {
proxy_cache cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 301 302 30m;
expires 30m;
proxy_pass http://www.domain.com:8080;
}
}
location /api/ {
alias /var/www/html/testphp/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}

Wordpress not loading on shared NGINX server name

I am successfully running a Wordpress installation alongside Meteor using NGINX. I have no real experience with Wordpress or php, so this may be a simple fix.
The following configuration works:
server_tokens off;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream mydomain-production {
server localhost:8000;
}
# redirect all non-subdomain traffic to https
server {
listen 80;
server_name www.mydomain.com mydomain.com;
rewrite ^ https://$host$request_uri? permanent;
access_log /var/log/nginx/mydomain.access.log;
error_log /var/log/nginx/mydomain.error.log;
}
# this non-subdomain serves meteor correctly
server {
listen 443 ssl spdy;
server_name www.mydomain.com mydomain.com;
access_log /var/log/nginx/mydomain.secure.access.log;
error_log /var/log/nginx/mydomain.secure.error.log debug;
ssl_certificate #...removed...# ;
ssl_certificate_key #...removed...# ;
ssl_stapling on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers #...removed...# ;
add_header Strict-Transport-Security "max-age=31536000;";
################################
# additional code will go here #
################################
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://mydomain-production;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
if ($uri != '/') {
expires 30d;
}
}
}
# this temporary subdomain serves wordpress correctly
server {
listen 80;
server_name wordpress.mydomain.com;
access_log /var/log/nginx/mydomain.access.log;
error_log /var/log/nginx/mydomain.error.log;
index index.php index.html index.htm;
root /var/www;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
So since I have Wordpress functioning on a temporary subdomain, I want to make it work on the same domain as Meteor and include location directives to route certain requests to Wordpress instead.
I tried adding the following to the 443 server name:
# additional code
location /blog {
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;
index index.php index.html index.htm;
}
location /wp-admin {
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;
index index.php index.html index.htm;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}
After doing this, I get an NGINX 404 page at mydomain/blog. So the location directive is successfully sending the request to /var/www instead of Meteor, but for some reason it is not getting to the Wordpress router. I have linked my NGINX error debug output here.
This was somehow solved by simply moving the root /var/www; and index index.php index.html index.htm; outside of the location directive(s). I would be interested to know why this is necessary if anyone can shed any light on this.

Categories