I have a server with nginx (php-fpm), but I need the index to be index.html.
My configuration is this:
server {
listen 80 default_server;
#SSL configuration listen *:443 ssl http2;
ssl_certificate /path/to/my.dev.pem;
ssl_certificate_key /path/to/my-key.pem;
root /home/path/to/misite.dev;
index index.php index.html index.htm;
server_name misite.dev ;
location / {
try_files index.html $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
add_header Cache-Control "public, max-age=31536000, immutable";
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
By having this, only the index.html works, but nothing with php, and by removing the location's index.html, everything works fine with php, but not the index.html
location / {try_files -> index.html <- $ uri / /index.php?$args;}
Do you know how I can do it so that both cases work?
(index.html and dynamic pages with php like mysite.com/tag/my-tag)
I hope I have explained well.
Using /index.html as the first parameter to try_files will break things.
The index directive will look for matching files in the order presented. See this document for details.
So if you have a directory containing more than one type of index file, simply construct your index statement in the order you would like Nginx to prioritise them.
For example:
index index.html index.php;
Related
If I have this config in sites-available in nginx for the aforementioned wordpress site, it will not display the root page (403 error), but it will display specific pages if I enter their URL manually, such as https:// subdomain.domain.tld/suchandsuch
server {
listen 80;
server_name subdomain.domain.tld;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
## Your website name goes here.
server_name subdomain.domain.tld;
## Your only path reference.
root /var/www/subdomain.domain.tld;
ssl_certificate /etc/letsencrypt/live/subdomain.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.tld/privkey.pem;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
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/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
#fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
However if I have this config set, it will do the opposite - it will show the root page, but any other pages I click on gives a 404 error:
server {
listen 80;
server_name subdomain.domain.tld;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name subdomain.domain.tld;
root /var/www/subdomain.domain.tld;
index index.php index.html;
access_log /var/log/nginx/subdomain.domain.tld-access.log;
error_log /var/log/nginx/subdomain.domain.tld-error.log;
ssl_certificate /etc/letsencrypt/live/subdomain.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.tld/privkey.pem;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Could some part of each config be needed to be combined together to make a working config for wordpress?
I expected the wordpress files and MySQL database to function as it had on the Apache server I had it on previously.
ChatGPT did a bang-up job of helping me figure this out.
Here is the final config:
server {
listen 80;
server_name subdomain.domain.tld;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name subdomain.domain.tld;
root /var/www/subdomain.domain.tld;
index index.php index.html;
access_log /var/log/nginx/subdomain.domain.tld-access.log;
error_log /var/log/nginx/subdomain.domain.tld-error.log;
ssl_certificate /etc/letsencrypt/live/subdomain.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.tld/privkey.pem;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
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/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
#fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
I have a nginx block for a wordpress installation, and a subdirectory which redirects to an app I have running. I want .php urls that go to the subdirectory to not be evaluated by fastcgi, since the app has it's own special system for evaluating them. However, php URLs still go to the fastcgi block and return a 404.
Nginx configuration:
server {
listen 0.0.0.0:443 http2 default_server;
listen [::]:443 http2 default_server;
ssl on;
ssl_certificate /etc/nginx/certificate.pem;
ssl_certificate_key /etc/nginx/certificate.key;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
root /var/www/wordpress;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /my_app {
proxy_pass http://localhost:8081/;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I want /index.php to be evaluated by fastcgi.
I want /my_app/index.php to be proxy_pass'd to my app.
After I changed ICG to nginx all routes except index page does not work.
Laravel Config:
#/etc/nginx/sites-enabled/laravel
server {
listen 80;
root /var/www/home;
index index.php;
server_name 192.168.178.71;
access_log /var/www/home/storage/app/logs/laravel-nginx-access.log;
error_log /var/www/home/storage/app/logs/laravel-nginx-error.log error;
location /home {
root /home/public;
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
# ERROR
error_page 404 /index.php;
# DENY HTACCESS
location ~ /\.ht {
deny all;
}
}
Default config:
# /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name 192.168.178.71 localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ index.php?$query_string;
autoindex on;
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
}
location ~ \.php$ {
#try_files $uri /index.php =404;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
my nginx config
#/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
disable_symlinks off;
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
What I tried:
/var/www/home# (home folder is laravel folder)
sudo chown -R www-data:www-data *
/var/www/home#
sudo chown -R root:root *
also I tried to change
try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ /index.php$is_args$args;
try_files $uri $uri/ /index.php;
php artisan cache:clear
Mostly questions in google i have read, but nothing helps me.
My phpinfo - link
This is the correct basic config for Laravel and Nginx:
server {
listen 80 default_server;
root /var/www/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EDIT:
Instead of:
fastcgi_pass unix:/var/run/php5-fpm.sock;
As of November 2018, as PHP 7.2 is out, it would be:
fastcgi_pass unix:/var/run/php7.2-fpm.sock;
When I sent parameters by get I did not recognize them, I just have to activate the following:
try_files $uri $uri/ /index.php$is_args$args;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
I had same problem after updating few lines nginx working fine..
It's for windows ( change root acording your file system )
1.root html/laravel; #Update Here - add project folder name after html
2.try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error
server {
listen 80; # IPv4
server_name localhost;
## Parametrization using hostname of access and log filenames.
access_log logs/localhost_access.log;
error_log logs/localhost_error.log;
## Root and index files.
root html/laravel; #Update Here - add project folder name after html
index index.php index.html index.htm;
## If no favicon exists return a 204 (no content error).
location = /favicon.ico {
try_files $uri =204;
log_not_found off;
access_log off;
}
## Don't log robots.txt requests.
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
## Try the requested URI as files before handling it to PHP.
location / {
try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error
## Regular PHP processing.
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_processes;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
## Static files
location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
expires max;
log_not_found off;
## No need to bleed constant updates. Send the all shebang in one
## fell swoop.
tcp_nodelay off;
## Set the OS file cache.
open_file_cache max=1000 inactive=120s;
open_file_cache_valid 45s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
}
## Keep a tab on the 'big' static files.
location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
expires 30d;
## No need to bleed constant updates. Send the all shebang in one
## fell swoop.
tcp_nodelay off;
}
} # / location
}
I had the same issue, but updating the default configuration made it work.
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location / {
try_files $uri $uri/ #rewrite;
}
Let me know if this worked for you or not.
sudo service nginx restart after changing the configuration.
Try it, work for me.
sudo nano /etc/nginx/sites-enabled/default
and then sudo systemctl reload nginx
server {
listen 80;
server_name _ midominioexample.com www.midominioexample.com;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
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; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I was also getting the same error of Routes not working on Nginx on my Ubuntu 16.04
To solve Routes problem, i tried the following code and its just working fine for me.
Open project conf file using following command
sudo nano /etc/nginx/sites-available/projectname
Then do the following changes in this file
server {
listen 80;
listen [::]:80;
root /var/www/project_name/public;
server_name server_name;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
Important thing is to change the try_files in location block.
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
I found that this solved my problem with Laravel routing.
I nested the location ~ .php$ inside location /.
Example:
server{
listen 9000;
server_name _;
root /var/www/myapp/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
}
As of 03/2022 and the current version of Laragon (5.0.0) I spent a lot of time to figure out why I can't open any link except index.php that I have configured in web.php for the route:list.
Because I just wanted to turn off SSL and turn it back on. It seems this causes to reset all your config files.
It seems that Laragon by default adds those lines:
# Access Restrictions
allow 127.0.0.1;
deny all;
I have put a # in front of deny to uncomment it and it worked again, like this:
# Access Restrictions
allow 127.0.0.1;
#deny all;
I'm running a Laravel Site (Ubuntu) on Nginx (Not a virtual box). When I make changes to a css file, or any file for that matter i'm unable to see the changes right away. I've tried changing sendfile from on to off as noted in this link:
How to clear the cache of nginx?
And I couldn't find a Nginx cache file to delete the cache. Many sites recommend going to the "path/to/cache" folder but I can't find it:
https://www.nginx.com/blog/nginx-caching-guide/
Anyway I don't believe I set up any kind of proxy caching or anything. Any ideas where I can find my cache so I can delete it folks? For that matter am I looking to do the correct thing here? Thanks for you answer in advance.
This is what my test server block looks like:
server {
# secure website with username and password in htpasswd file
auth_basic "closed website";
auth_basic_user_file /tmp/.htpasswd.txt;
listen (myip) default_server;
listen [::]:83 default_server ipv6only=on;
root /var/www/test/(sitefolder);
index index.php index.html index.htm;
# Make site accessible
server_name (myiphere);
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri /index.php =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;
include fastcgi_params;
}
This is what my real site server block looks like:
server {
# secure website with username and password in htpasswd file
auth_basic "closed website";
auth_basic_user_file /tmp/.htpasswd.txt;
listen (myip) default_server;
listen [::]:81 default_server ipv6only=on;
root /var/www/(mysite)/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name (myip);
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri /index.php =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;
include fastcgi_params;
}
First, css is something that the client caches, not the server. You should clear your browser's cache to see your css changes. Furthermore, browser caching is a common problem in css and javascript development. Luckily, you can take advantage of elixir, which ships with laravel 5.1, to prevent browsers caching your assets.
Visit the docs for more info on laravel elixir.
http://laravel.com/docs/5.1/elixir#versioning-and-cache-busting
Second, laravel gives us a convenient command to clear your server's cache:
php artisan cache:clear
UPDATE:
server {
listen 80;
server_name example.com;
root "/usr/share/nginx/app/public";
index index.html index.htm index.php;
charset utf-8;
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/app-error.log error;
client_max_body_size 100m;
include hhvm.conf;
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name beta.example.com;
root "/usr/share/nginx/beta/public";
index index.html index.htm index.php;
charset utf-8;
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/app-error.log error;
client_max_body_size 100m;
include hhvm.conf;
location ~ /\.ht {
deny all;
}
}
Ran into a 500 issue when running Nginx and WP together and setting pretty permalinks. I've been trying a bunch of different methods from Google but none seems to help.
Config -
server {
listen 80;
root /var/www/mydomain.com/public_html;
index index.php index.html index.htm;
server_name .mydomain.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
All files load perfectly well, and the pages work if using the default permalinks setting. Strange thing is, if I check the network log I first see a 200 OK being received, then immediately followed by a 500. Any ideas?
Edit: Setting to close as I'm switching to Apache instead. Will mark correct answer as it seems to have helped others.
try:
try_files $uri $uri/ /index.php?q=$uri&$args;
AND:
fastcgi_index /index.php;
(note the / )
Also above answer by Rufinus is correct.
Here is correct . You Forget to define Index . So nginx don't know which file to lockup and process. Hope this help u out.
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Your Config does not have fastcgi_parm .
add this line to your config. Its main part of Nginx Config.
change /home/public_html with your website path location
fastcgi_param SCRIPT_FILENAME /home/public_html$fastcgi_script_name;
Here is my Whole wordpress config file. I Hope that works. Its working Nginx config file with pretty urls.
server {
listen 80;
server_name example.com;
root /home/public_html;
index index.php index.htm index.html;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location /favicon.ico { access_log off; log_not_found off; }
location ~* \.(jpg|jpeg|gif|png|js|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/public_html$fastcgi_script_name;
include fastcgi_params;
}
}