I have this NGINX conf file,
If I load any root url, it will show my default.jpg image,
but all of my mail images are stores in subfolders, eg:
/missing.jpg // works
/images/mising.jpg // shows nginx 404 not /default.jpg
Here is my nginx conf.
Note: either I can use: /default.jpg or /index.php which outputs the same image..
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php;
server_name _;
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
add_header Cache-control "public";
access_log off;
expires 365d;
}
location / {
#try_files $uri $uri/ default.jpg;
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
I solved this by adding the try_files to the image cache block.
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
add_header Cache-control "public";
access_log off;
expires 365d;
try_files $uri /index.php$is_args$args;
}
Related
I'm trying to serve WordPress from the subdirectory site.com/blog but im facing 404, tried different configurations but none of them are working getting a 404.
The WordPress files are at /home/ubuntu/wordpress
Here is my Nginx configuration, I'm serving static files for the root domain site.com and running Laravel for backend API, I think it conflicts with the FastCGI settings, not sure though.
server {
root /home/ubuntu/mysite/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name mysite.com www.mysite.com;
access_log off;
include /etc/nginx/badbots.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|json|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
expires 365d;
add_header Cache-Control "public, max-age:24h";
}
large_client_header_buffers 4 32k;
location /blog {
alias /home/ubuntu/wordpress;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
listen 80;
}
I have the following domain:
https://example.com
I want, when the following /path/ is hit:
https://example.com/path/subpath/?param1=value1¶m2=value2
the content from this url to be served:
https://example.com/subpath/?param1=value1¶m2=value2
without performing a redirect.
I have tried using an alias like this:
location /path/ {
alias /home/forge/example.com/current/;
}
where current is a symlink pointing to the latest release:
current -> /root/example.com/releases/timestamp/
But it doesn't work. it gives 404.
How can I achieve this?
The entire server block:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
server_tokens off;
root /home/forge/example.com/current;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location /path/ {
alias /home/forge/example.com/current/;
}
location / {
gzip_static on;
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 /var/log/nginx/example.com-access.log;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
gzip on;
}
I don't see any reason why
rewrite ^/path(/.*) $1;
didn't work as expected. But if you want to use an alias solution instead, you should take into account that regex matching locations have a greater priority than prefix ones and any request for /path/subpath/index.php would be processed by location ~ \.php { ... } rather than location /path/ { ... }. You can override this with ^~ location modifier using a second nested PHP handler:
location ^~ /path/ {
alias /home/forge/example.com/current/;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
On my Nginx webserver i have several virtual hosts like this:
- api.example.com
- www.example.com
- cv.example.com
But when i am visiting www.example.com/example and this is not an valid path its giving me 404 page of my api.example.com. But why ?
This is my current nginx configuration of www.example.com :
server {
listen 443 ssl;
listen [::]:443 ssl;
access_log /var/log/nginx/www.example.com-access.log timed;
error_log /var/log/nginx/www.example.com-error.log;
root /var/www/wwwexamplecom/html/_site;
server_name example.com www.example.com;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
location / {
index index.html index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/example/html/public/index.php;
include fastcgi_params;
}
location ~ /.well-known {
allow all;
}
}
This is the configuration of my api.example.com :
server {
listen 443 ssl;
listen [::]:443 ssl;
access_log /var/log/nginx/api.example.com-access.log timed;
error_log /var/log/nginx/api.example.com-error.log;
root /var/www/apiexamplecom/html/public;
server_name api.example.com;
include snippets/ssl-api.example.com.conf;
include snippets/ssl-params.conf;
location / {
index index.html index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/apiexamplecom/html/public/index.php;
include fastcgi_params;
}
location ~ /.well-known {
allow all;
}
}
I think self its in the / location part but i am not really how i can fix this issue. This is also happening on other virtualhost.
You have to explicitly let nginx know that you want to throw a 404 error if it can't find anything in the location block. Otherwise it will try matching your 'default' server block (which in this case seems to be the api.example.com one because you haven't specified the default). It can't find anything in that server either, THEN it tries the 404.
To explicitly tell nginx to throw a 404 if it can't find anything in your location block, add =404 to the end of your try_files line. This means that if it can't find any files for $uri, $uri/ or any php files, then throw the 404 without trying anything else.
try_files $uri $uri/ /index.php?q=$uri&$args =404;
Whenever I put a known file extension in the url nginx return 404 Not Found.
domain.com/myroute.foo and domain.com/foo/myroute.foo is fine, but domain.com/myroute.php and domain.com/foo/myroute.php (or for example .css, .js) returns 404 Not Found.
My nginx server config:
server {
listen 80;
server_name domain.com;
root /var/www/path/public;
charset utf-8;
gzip on;
#access_log logs/host.access.log main;
location / {
index index.html index.php index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
access_log off;
}
}
Why isn't a url with a known file extension (/myroute.php) going to my index.php file like any other url?
myroute.php does not exist on your server.
Nginx location directives are checked in this order
Directives with the "=" prefix that match the query exactly (literal string). If found, searching stops.
All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
Regular expressions, in the order they are defined in the configuration file.
If #3 yielded a match, that result is used. Otherwise, the match from #2 is used
That means that your myroute.php request gets handled by the ~ \.php$ location block, which results in a 404 as per your try_files directive.
To solve this, you'll need to either make your location directive more specific (e.g. ~ index\.php$), or try_files exactly as you do in the location /. Using a rewrite could also solve your issue.
EDIT:
It is important to understand in what order nginx chooses location blocks over other location blocks. See more at the nginx wiki
Regarding your question, the easiest solution I think is to use try_files
try_files $uri $uri/ /index.php?q=$uri&$args;
in both your location ~ \.php$ { and location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) { blocks
Note: don't forget to remove your old try_files $uri =404 from the .php$ block
Your final conf file should now look like this
server {
listen 80;
server_name domain.com;
root /var/www/path/public;
charset utf-8;
gzip on;
#access_log logs/host.access.log main;
location / {
index index.html index.php index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
try_files $uri $uri/ /index.php?q=$uri&$args;
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
access_log off;
}
}
I'm using Wordpress as root of my website and Invision Power Boards as forum.
http://localhost -> Wordpress
http://localhost/forum -> IPB
I have removed "index.php" from Wordpress URLs successfully with Nginx-rewrite however when I try to use SEO Friendly URLs on IPB, nginx simply returns to Wordpress' 404 page.
My configuration is like this:
#This removes "index.php" from Wordpress URLs
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Then I follow this link to modify my nginx conf file in order to be able to use SEO friendly URLs of IPB: http://www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-and-nginx/
#This part is to be able to use IPB SEO
location /forum/ {
index index.php;
try_files $uri $uri/ /forum/index.php?$uri&$args;
rewrite ^ /index.php? last;
}
When I click a link on my forum (for example: http://localhost/forum/index.php/forum/51-sport/) nginx simply redirects me to (http://localhost/forum/forum/51-sport/) which displays Wordpress 404 error page.
I have very little knowledge about regex so any help would be appreciated.
This is my whole conf file after modifications, little messy I accept that.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/user_name/public_html;
access_log /var/log/nginx/a/access.log;
error_log /var/log/nginx/a/error.log
server_name localhost;
server_tokens off;
location / {
try_files $uri $uri/ #wordpress;
}
location #wordpress {
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME /index.php;
}
location /forum {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
location /forum/ {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
#location / {
#index index.php index.html index.htm;
#try_files $uri $uri/ /index.php?q=$uri&$args;
#}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/)(/.*)$;
}
# Add trailing slash to */wp-admin and */forum requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9001
#location ~ \.php$ {
# fastcgi_split_path_info ^(/)(/.*)$;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
# fastcgi_param PATH_INFO $fastcgi_script_name;
# include /etc/nginx/fastcgi_params;
#REMOVE THIS
#fastcgi_read_timeout 60000;
#fastcgi_send_timeout 6000;
#}
}
Since the last post, I have played with IPB's SEO configurations and I managed to remove "index.php" from URLs. It doesn't effect the result of course. But it seems that location / decides what to do and therefore link is being considered as a Wordpress permalink.
EDIT - Solution
# Upstream to abstract backend connection(s) for php
upstream php {
# server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9001;
}
server {
## Your website name goes here.
server_name localhost;
## Your only path reference.
root /home/username/public_html;
## 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
try_files $uri $uri/ /index.php;
}
location /forum {
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? break;
}
location ~ ^/forum/index.php {
if ($args != "") {
rewrite ^ http://www.google.com/ permanent;
}
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? last;
}
location /forum/admin/ {
try_files $uri $uri/ /forum/admin/index.php;
rewrite ^ /forum/admin/index.php? last;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include /etc/nginx/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
I'm too using wordpress and ipb on nginx, now configuring, i added wordpress permalink to ipb config and turn on seo Rewrite URLs, Force Friendly URLs
location / {
try_files $uri $uri/ /index.php?$args;
}
it worked for me