Is there a way I can set an absolute path for nginx error_pages? Not absolute as in http://, but absolute as in /usr/var/nginx/errors/500.html.
Sure you can but in an indirect way:
error_page 500 /500.html;
location = /500.html {
root /usr/var/nginx/errors;
allow all;
internal;
}
see http://wiki.nginx.org/HttpCoreModule#error_page
Related
I have a Mediawiki farm and everything works except image display. Image upload works in the sense that they get put in the correct folder along with thumbs, but images don't display. I'd like to keep hosting images outside of site root though.
The Mediawiki installation is in: /var/www/mediawiki
The image folder is in: /var/cats.wiki/images
My nginx config is:
server {
listen 80;
server_name cats.wiki; #made up name for example
root /var/www/mediawiki;
client_max_body_size 100M;
location /images {
alias /var/cats.wiki/images; #relevant part
}
location / {
index index.php;
error_page 404 = #mediawiki;
}
location #mediawiki {
rewrite ^/w([^?]*)(?:\?(.*))? /index.php?title=$1&$2 last;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MW_INSTALL_PATH /var/www/mediawiki;
fastcgi_param WIKI_PATH "catwiki.php";
}
location ~* \.(js|css|svg|png|jpg|jpeg|gif|ico)$ {
try_files $uri /index.php;
expires 365d;
log_not_found off;
gzip_static on;
gzip_comp_level 5;
access_log off;
add_header Cache-Control private;
}
}
and the relevant section of my LocalSettings, the logo also doesn't display on browser
$wgLogo = "/var/cats.wiki/images/logo.png";
$wgEnableUploads = true;
$wgUseImageMagick = true;
$wgImageMagickConvertCommand = "/usr/bin/convert";
$wgUploadDirectory = "/var/cats.wiki/images";
$wgUploadPath = "/images";
Thanks! :)
It is not clear from your question whether the server directive from your nginx config applies to your whole farm or only one wiki. You definitely can have one server for all wikies; I do in my own wiki farm setup.
In my wiki farm setup, the section for the image folder says (simplified and adapted to your example):
location ~* ^/images(?<image_subpath>/.+)$ {
root $images_root;
try_files $image_subpath #mediawiki;
# ... (some code to neutralise potentially malicious uploads)
}
where $images_root is set previously, in http directive, with a map directive (simplified and adapted to your example):
map $host $images_root {
cats.wiki /var/cats.wiki/images;
dogs.wiki /var/dogs.wiki/images;
# ...
}
I have a nginx virtual server with a custom 503 used for maintenance.
The html page uses some fonts from google fonts, bootstrap, but also a css and an image from the local server.
When the page is loaded, the linked css is not loaded. Using Firefox web tools I can see for the Error 503.
Trying direclty the css URL, the css is loaded by the browser.
The image is correclty displayed even if has an error 503.
This is my nginx configuration:
if (-f /home/user-site/www/MainWebSite/releases/current/.maintenance) {
return 503;
}
if (!-d /home/user-site/www/MainWebSite/releases/current) {
return 503;
}
# error_page 503 /maintenance.html;
# location = /maintenance.html {
# root /home/user-site/www/MainWebSite/htdocs;
# }
error_page 503 #maintenance;
location #maintenance {
root /home/user-site/www/MainWebSite/htdocs;
#rewrite ^(/css)/(.+?)(\.[^.]*$|$) /stuff/css/$2$3 break;
rewrite ^(/img)/(.+?)(\.[^.]*$|$) /stuff/img/$2$3 break;
rewrite ^(.*)$ /maintenance.html break;
}
I suppose that the css cannot be loaded because of the error 503 returned by the server even if the rewrite roule correctly retunr the css.
How I can prevent error 503 on css and images of landing pages?
tanks.
Update!!
Following this solution the configuration works!
if (-f /home/user-site/www/MainWebSite/releases/current/.maintenance) {
set $err503 1;
}
if (!-d /home/user-site/www/MainWebSite/releases/current) {
set $err503 1;
}
error_page 503 #maintenance;
location #maintenance {
root /home/user-site/www/MainWebSite/htdocs/error503;
try_files $uri /maintenance.html =503;
}
location /static {
root /home/user-site/www/MainWebSite;
}
location /media {
root /home/user-site/www/MainWebSite;
}
location /robots.txt {
root /home/user-site/www/MainWebSite/htdocs/robots.txt;
}
location /sitemap.xml {
root /home/user-site/www/MainWebSite/htdocs/sitemap.xml.txt;
}
location ~* \.(css) {
root /home/user-site/www/MainWebSite/htdocs/error503;
# The file will be returned
}
location ~* \.(png|jpg|jpeg) {
root /home/user-site/www/MainWebSite/htdocs/error503;
# The file will be returned
}
location / {
if ($err503 = 1) {
return 503;
}
uwsgi_pass unix:/tmp/uwsgi_MainWebSite.sock;
include /etc/nginx/uwsgi_params;
}
I have an existing server which is working well hosting a number of sites using nginx and ISPconfig. However I have created a new site and wish to use Laravel.
I've installed Laravel successfully via composer and have got as far as seeing the familiar welcome blade displayed when I visit mywebsite.com/public
What I want to do next is make some clean urls. My experience with vhost files is somewhat limited and I'm having a bit of trouble with the config.
My routes file looks like this
Route::get('/', function () {
return view('welcome');
});
Route::get('/test', function () {
return view('test');
});
and I'd hoped to see mywebsite.com/test display the contents of test.blade.php
I'm aware I need to do some work with the vhost file before I can expect this to work but my experience with vhosts is limited and I'm at a bit of a loss.
My current file looks like this
server {
listen *:80;
server_name mywebsite.com ;
root /var/www/mywebsite.com/web;
index index.html index.htm index.php index.cgi index.pl index.xhtml;
error_page 400 /error/400.html;
error_page 401 /error/401.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 405 /error/405.html;
error_page 500 /error/500.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
recursive_error_pages on;
location = /error/400.html {
internal;
}
location = /error/401.html {
internal;
}
location = /error/403.html {
internal;
}
location = /error/404.html {
internal;
}
location = /error/405.html {
internal;
}
location = /error/500.html {
internal;
}
location = /error/502.html {
internal;
}
location = /error/503.html {
internal;
}
error_log /var/log/ispconfig/httpd/mywebsite.com/error.log;
access_log /var/log/ispconfig/httpd/mywebsite.com/access.log combined;
location ~ /\. {
deny all;
access_log off;
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 /stats/ {
index index.html index.php;
auth_basic "Members Only";
auth_basic_user_file /var/www/clients/client1/web5/web/stats/.htpasswd_stats;
}
location ^~ /awstats-icon {
alias /usr/share/awstats/icon;
}
location ~ \.php$ {
try_files /5e26a1d85cb98f7191261e023385e60d.htm #php;
}
location #php {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/lib/php5-fpm/web5.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
Now on another server I have this working with this simple directive
server {
root /var/www/public;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
But I am limited to what I can do with the vhost on the current server as ISPconfig writes most of it for me and it refuses to write the above config that worked elsewhere. Also I feel editing the file directly will be bad practice, I'd always be on edge that ISPconfig will rewrite the file for me, so I'm not really sure how best to proceed with this.
My options would be to just go ahead and edit the vhost and hope for the best, but if I do that how would I ensure ISPconfig could not overwrite the file without resorting to "hacky" methods?
Alternatively, is there a config I can enter via ISPconfig that will allow rewrites to happen properly in a way that suits Laravel? In this instance, any directive entered would need to take precedence over the ~ .php$ clause as that is written by ISPconfig before any directives entered via the control panel.
I just had the same problem recently. Digging the ISPConfig's sources, I understood it can insert/ merge/ delete location blocks of that default vhosts file. So i did the following:
Sites' menu > choose website > Options
Then I inputed the following on the "nginx Directives" field:
# redirect stuff to the public inner folder
location / {
root {DOCROOT}/public;
try_files /public/$uri /public/$uri/ /public/index.php?$query_string;
}
# merged the stuff people suggests for laravel inside the php block
# mind the 'merge' keyword that did the trick
location ~ \.php$ { ##merge##
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;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
There is a slight problem with Danilo's answer. Php ran but assets like js/css/images stopped loading. Adding the following to nginx directives inside ISPConfig works for me:
location / {
root {DOCROOT}/public;
try_files $uri public/$uri/ /public/index.php?$query_string;
}
I am not expert with this but from my past experience if I have two domains I would define server blocks for each in two different files and place them in /etc/nginx/sites-available/site1.com and /etc/nginx/sites-available/site2.com
but it looks like you already have a website that you access using mywesite.com which is located at /var/www/mywebsite.com/web; (see the root value of your configuration file)
Now you install Laravel in test folder in /var/www/mywebsite.com/test location.
To access this you need can try adding following at the end of your ispconfig file.
Note how I used the relative path to laravel's public folder from the root of the server block.
location /../test/public {
try_files $uri $uri/ /index.php$is_args$args;
}
For more detailed tutorial try Nginx Server Block Setup.
Hope this helps,
K
How can I configure nginx to redirect all URL's (not prepended with /api or some static resource eg. JS/images) to index.html? Reason is I am using HTML5 push state URL's with a single page application. Meaning content is changed whether AJAX or JS depending on the URL
My current nginx config looks like:
server {
listen 2000;
server_name localhost;
location / {
root /labs/Projects/Nodebook/public;
index index.html;
}
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_redirect off;
}
}
location / {
try_files $uri /index.html;
}
This will check if the requested file exists and return it. If the file doesn't exist, it will return index.html.
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
mattes answer is almost a solution, however it won't give 404 for missing files (e.g. favicon.icon) as aschepis pointed out.
Nginx will pick the first location that matches. So we can first match for files (which will give 404 if the file does not exist). And after put a location which defaults to index.html for all urls.
location /.+\..+ { # files (assuming they always have a dot)
# use eg alias to serve some files here
}
location / { # url routed by client, client gives 404 for bad urls
try_files $uri /index.html;
}
You need to add to your nginx config file:
rewrite ^(.+)$ /index.html last;
Then say you're using Backbone.js just make sure you re-route any non-defined route to a 404 page:
routes: {
// Other routes
"*path" : "notFound"
},
notFound: function(path) {
// Load 404 template, probably of a cute animal.
}
Source:
http://readystate4.com/2012/05/17/nginx-and-apache-rewrite-to-support-html5-pushstate/
How to set index.html for the domain name e.g. https://www.example.com/ - leads user to index.html in root directory.
I've tried different things like:
server {
# some configs
location = / {
index index.html;
fastcgi_index index.html;
}
or
location / {
index index.html;
fastcgi_index index.html;
}
}
Nothing helped me.
There are some other configs with location keyword, though I'd commented them either.
Other "location" configs in the server { clause:
location ~ .*(css|htc|js|bmp|jp?g|gif|ico|cur|png|swf|htm?|html)$ {
access_log off;
root $www_root;
}
location ~ \.php$
{
include /etc/nginx/fastcgi_params;
index index.html;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME $www_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
# Директива определяет что ответы FastCGI-сервера с кодом больше или равные 400
# перенаправлять на обработку nginx'у с помощью директивы error_page
fastcgi_intercept_errors on;
break;
}
location ~ /\.ht {
deny all;
}
All them were commented and uncommented, but nothing helped.
PS Editions were made in /etc/nginx/sites-enabled/domainname.com file.
in your location block you can do:
location / {
try_files $uri $uri/index.html;
}
which will tell ngingx to look for a file with the exact name given first, and if none such file is found it will try uri/index.html. So if a request for https://www.example.com/ comes it it would look for an exact file match first, and not finding that would then check for index.html
location / { is the most general location (with location {). It will match anything, AFAIU. I doubt that it would be useful to have location / { index index.html; } because of a lot of duplicate content for every subdirectory of your site.
The approach with
try_files $uri $uri/index.html index.html;
is bad, as mentioned in a comment above, because it returns index.html for pages which should not exist on your site (any possible $uri will end up in that).
Also, as mentioned in an answer above, there is an internal redirect in the last argument of try_files.
Your approach
location = / {
index index.html;
is also bad, since index makes an internal redirect too. In case you want that, you should be able to handle that in a specific location. Create e.g.
location = /index.html {
as was proposed here. But then you will have a working link http://example.org/index.html, which may be not desired. Another variant, which I use, is:
root /www/my-root;
# http://example.org
# = means exact location
location = / {
try_files /index.html =404;
}
# disable http://example.org/index as a duplicate content
location = /index { return 404; }
# This is a general location.
# (e.g. http://example.org/contacts <- contacts.html)
location / {
# use fastcgi or whatever you need here
# return 404 if doesn't exist
try_files $uri.html =404;
}
P.S. It's extremely easy to debug nginx (if your binary allows that). Just add into the server { block:
error_log /var/log/nginx/debug.log debug;
and see there all internal redirects etc.
The answer is to place the root dir to the location directives:
root /srv/www/ducklington.org/public_html;
According to the documentation
Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made. Important
an internal redirect to the uri specified in the last parameter is
made.
So in last parameter you should add your page or code if first two parameters returns false.
location / {
try_files $uri $uri/index.html index.html;
}
Add this to the location block in nginx works for me
try_files $uri $uri/ /index.html =404;
thats the entire block
location / {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
root /var/www
try_files $uri $uri/ /index.html =404;
}
For me, the try_files directive in the (currently most voted) answer https://stackoverflow.com/a/11957896/608359 led to rewrite cycles,
*173 rewrite or internal redirection cycle while internally redirecting
I had better luck with the index directive. Note that I used a forward slash before the name, which might or might not be what you want.
server {
listen 443 ssl;
server_name example.com;
root /home/dclo/example;
index /index.html;
error_page 404 /index.html;
# ... ssl configuration
}
In this case, I wanted all paths to lead to /index.html, including when returning a 404.