.php files are downloaded instead of displayed - nginx

First of all, yes there are lots of questions like these but none of the solutions worked for me.
I have a similar server to compare to and I looked at the configs and it seems to work, whereas this new setup doesn't.
I already tried mendling with the configs without any prevail.
My nginx.conf:
[default content]
#I just appended this line:
client_max_body_size 10M;
My virtual host file:
server {
listen 80;
listen [::]:80;
# root /var/www/mywebsite.com/public_html;
# index index.html;
server_name mywebsite.com www.mywebsite.com;
# access_log /var/log/nginx/mywebsite.com.access.log;
# error_log /var/log/nginx/mywebsite.com.error.log;
include snippets/letsencrypt.conf;
return 301 https://$host$request_uri;
# location / {
# try_files $uri $uri/ =404;
}
server {
listen 443 ssl http2;
server_name www.mywebsite.com;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/mywebsite.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;
return 301 https://mywebsite.com$request_uri;
}
server {
listen 443 ssl http2;
server_name mywebsite.com;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/mywebsite.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;
root /var/www/mywebsite.com/public_html;
index index.php;
# . . . other code
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
#location / {
# try_files $uri $uri/ /index.php?$args;
#}
#location ~ \.php$ {
# fastcgi_split_path_info ^(/wordpress)(/.*)$;
#}
include snippets/phpmyadmin.conf;
}
P.S Yes, I am using WordPress.

So it seems that it was just a cache issue so I had to wait and it worked afterwards. The config is OK. Case closed.

Related

Nginx Multiple Domains Same IP Not Working

I am having issues with Nginx not working with multiple domains pointing to the same IP address. When accessing either domain, they both default to "catacombsfellowship.org.cnf". When accessing edmunddesoto.com, it acts as if it is catacombsfellowship.org and I get an SSL cert error where the cert is for catacombsfellowship.org.
When I run either domain by themselves, they work fine. However, they don't work seamlessly together. What am I doing wrong?
Examples:
File: edmunddesoto.com.cnf
server {
listen 80;
listen [::]:80;
server_name edmunddesoto.com default_server;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name edmunddesoto.com default_server;
root /var/www/html/edmunddesoto.com/live;
index index.php index.html index.htm index.nginx-debian.html;
ssl_certificate /etc/ssl/certs/edmunddesoto.com.crt;
ssl_certificate_key /etc/ssl/private/edmunddesoto.com.key;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
File: catacombsfellowship.org.cnf
server {
listen 80;
listen [::]:80;
server_name catacombsfellowship.org default_server;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name catacombsfellowship.org default_server;
root /var/www/html/catacombsfellowship.org/live;
index index.php index.html index.htm index.nginx-debian.html;
ssl_certificate /etc/ssl/certs/catacombsfellowship.org.crt;
ssl_certificate_key /etc/ssl/private/catacombsfellowship.org.key;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Ok, I figured it out. The issue was somehow it was redirecting to the www.* domains and those were not set in the server_name directive. So, by adding the www.* to the server_name directive, it fixed it.

How to redirect https://www to non-www https version in Nginx

I need some help with my nginx config so all my http traffic goes to https and also www changes to non-www. My server is running ubuntu 18.04 and nginx.
here is my sites-available .conf file
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name cdn.example.com;
return 301 https://cdn.example.com$request_uri;
}
server {
listen 443 http2;
ssl on;
ssl_certificate /etc/letsencrypt/live/cdn.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/cdn.example.com/privkey.pem; # managed by Certbot
root /var/www/html/storage;
index index.php index.html index.htm;
server_name cdn.example.com;
location ~* \.(jpg|jpeg|png|ico|js|css|json)$ {
expires 365d;
}
location / {
aio threads;
limit_rate_after 1m;
limit_rate 3m;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/letsencrypt/live/example.com-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem; # managed by Certbot
}
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/example.com-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem; # managed by Certbot
root /var/www/html/;
index index.php index.html index.htm;
server_name example.com www.example.com;
rewrite ^/index.php/(.*) /$1 permanent;
location ~* \.(jpg|jpeg|png|ico|js|css|json)$ {
expires 365d;
}
location / {
aio threads;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
I tried so many different configuration but none of them worked for me.

Nginx HTTPS SSL redirection doesn't work in Ubuntu 18.04

I've tired to configure an Nginx server with SSL but the site is not open but with https:// it's open normally.
Here is my Nginx configuration:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
client_max_body_size 20M;
root /var/www/mysite.in/site;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
listen 443;
listen [::]:443;
server_name www.mysite.com;
#ssl on;
ssl_certificate /etc/nginx/ssl/mysite.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/main_private.key;
}
The SSL was generated in GoDaddy, I've found lots of solutions, but so far none of them are working.
How can I resolve this error?
You have to create two servers(http and https) in your config and create redirect from http to https:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.mysite.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
listen [::]:443;
server_name www.mysite.com;
client_max_body_size 20M;
root /var/www/mysite.in/site;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
ssl on;
ssl_certificate /etc/nginx/ssl/mysite.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/main_private.key;
}

Minimizing and optimizing NGINX vhost config

Is it possible to optimize/minimize the config posted below?
I feel that it should be possible to merge all the redirects into something more simple.
http:// & http://www & https://www > https://
Though I've had issues and settled.
I understand variables are not supported in NGINX config, so I have to manually define the log locations for example. Would there be a way to set a default location for all vhosts?
I use the same ssl-params.conf file for all vhosts. Can this be defaulted and disabled on a per-vhost basis?
# Redirect http:// & http://www to https://
server {
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
# Redirect https://www to https://
server {
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com/$request_uri;
}
# Main config
server {
listen 443 ssl;
server_name example.com;
# SSL config
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
# Error logs
access_log /srv/logs/nginx.access.example.com.log;
error_log srv/logs/nginx.error.example.com.log;
# Root dir
location / {
root /srv/example.com/_site/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
# Caching
location ~ .php$ {
root /srv/example.com/_site/;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
root /srv/example.com/_site/;
expires 365d;
}
location ~* \.(pdf)$ {
root /srv/example.com/_site/;
expires 30d;
}
# SSL
location /.well-known {
allow all;
}
}
I understand variables are not supported in NGINX config, so I have to manually define the log locations for example. Would there be a way to set a default location for all vhosts?
Yes, just define it in the http context of your config or stick with the default of your distro (e.g. /var/log/nginx/access.log).
I use the same ssl-params.conf file for all vhosts. Can this be defaulted and disabled on a per-vhost basis?
It works the other way around you enable it where you need it through the include directive.
Here is a shorter config (untested):
http {
error_log /srv/logs/nginx.error.example.com.log;
access_log /srv/logs/nginx.access.example.com.log;
index index.php index.html index.htm;
server {
listen 80;
listen 443 ssl;
server_name .example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
root /srv/example.com/_site/;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
location / {
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
}
location ~* \.(jpe?g|png|gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(pdf)$ {
expires 30d;
}
try_files $uri $uri/ /index.php?$args;
}
location /.well-known {
allow all;
}
}
}

Nginx server only shows default page

I have a problem with Nginx server as it only shows default page. Virtual host and host file seem to be ok. I don't get where is the problem.
Here is my virtual host configuration:
server {
listen 80;
listen [::]:80;
listen 443 default ssl;
server_name marketplace_unirgy;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
root /var/www/html/marketplace_unirgy/;
index index.php;
#location / {
# index index.html index.php;
# autoindex on;
# #If missing pass the URI to Magento's front handler
# try_files $uri $uri/ #handler;
# expires max;
#}
#need it to execute php
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
## Magento uses a common front handler
location #handler {
rewrite / /index.php;
}
}
And my host:
127.0.0.1 marketplace_unirgy localhost
My website is in /var/www/html/marketplace_unirgy
You seem to have the default location commented out for some reason. Try enabling it with:
location / {
try_files $uri $uri/ /index.php;
}
See this and this for more.

Resources