This is a snippet of my nginx configuration for Drupal:
server {
listen 80;
server_name _;
root /home/testing/public_html/staging;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location = /backup {
deny all;
}
location ~* \.(txt|log)$ {
deny all;
}
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/tmp/php-fpm.sock;
}
location ~ ^/sites/.*/files/imagecache/ {
try_files $uri #rewrite;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
rewrite ^/staging/(.*)$ /$1;
expires max;
log_not_found off;
}
}
This works perfectly fine with URLs like these
http://www.testing.com/this/page
http://www.testing.com/that/page
until a hard-coded URL containing "/staging/" is processed. Example:
http://www.testing.com/staging/this/page
This displays the "Page not found" page. I tried adding this line:
location /staging {
rewrite ^/staging/(.*)$ /index.php?q=$1;
}
but this doesn't appear to work at all. How do I catch all URLs with "/staging/" and rewrite them properly so I don't get the "Page not found" error?
You need to add last to trigger another round of location match. Without it, your request is trapped in the /staging location block.
location /staging {
rewrite ^/staging/(.*)$ /index.php?q=$1 last;
}
Check your website configuration by trying this one.
Here is an example of working Nginx configuration
If it doesn't work, post your error logs.
Related
I'd like to install a software in a subdirectory of an NginX Vhost.
Currently I've got:
server {
server_name www.website.com;
root /var/www/website.com/www.website.com/;
gzip_static on;
access_log /var/log/nginx/website.com_access.log;
error_log /var/log/nginx/website.com_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
location ~ (^|/)\. {
return 403;
}
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^ /index.php;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_read_timeout 150;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri #rewrite;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Now for the subdirectory I tried:
location /mseiten/ {
try_files $uri $uri/ /mseiten/index.php?q=$request_uri;
allow all;
root /var/www/website.com/www.website.com;
}
But all I'm seeing is a 403 if I try to access /mseiten/ in my browser (it should direct me to my installation wizard). I've uncommented all rules that return 403; but with the same results.
Would it be possible to setup sth. like a clean slate for a sub directory?
So I have decided to do two kind of wrong, slightly inspired by Troy Hunt.
I would like my API to accept version specification through Accept header but also in the URL, e.g. /v1.
For now, I have made an nginx config which works with Accept header, but trying out various methods, I have not been able to get the /v1
What I want to achieve is that the URL passed to my application, does not include the version part as the role of the version is simply to point at a root directory.
map $http_accept $api_version {
default 0;
"application/vnd.it.echo.api+json; version=1" "v1";
}
server {
listen 80;
server_name api.app;
index index.html index.htm index.php;
charset utf-8;
sendfile off;
rewrite_log on;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location / {
if ($api_version = 0) {
return 307 https://echo.it;
}
try_files $uri $uri/ #api;
}
location /v1 {
set $api_version "v1";
rewrite ^/.+/(.+)$ /$1 last;
}
location #api {
root /home/vagrant/api/$api_version/public/;
error_log /var/log/nginx/api.$api_version.app-error.log error;
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
root /home/vagrant/api/$api_version/public/;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
The problem appears to be with the rewrite rule in the /v1 location directive not taking effect.
Images are only displaying if they have the correct case. These need to be case insensitive please see my config file.
The image I have having trouble with works at:
http://domain.com/sites/default/files/vimages/imagename.jpg
but not at
http://domain.com/sites/default/files/vimages/imagename.JPG
server {
listen 80;
server_name domain.com;
root /home/domain.com/www; ## <-- Your only path reference.
access_log /home/domain.com/logs/access.log;
error_log /home/domain.com/logs/error.log;
# Enable compression, this will help if you have for instance advagg‎ module
# by serving Gzip versions of the files.
gzip_static on;
gzip on;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# This matters if you use drush
location = /backup {
deny all;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
# No no for private
location ~ ^/sites/.*/private/ {
return 403;
}
# Block access to "hidden" files and directories whose names begin with a
# period. This includes directories used by version control systems such
# as Subversion or Git to store control files.
location ~ (^|/)\. {
return 403;
}
location / {
# This is cool because no php is touched for static content
try_files $uri #rewrite;
}
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 180;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 52w;
add_header Cache-Control "max-age=31449600, no-transform, public";
log_not_found off;
}
# Catch image styles for D7 too.
location ~* ^/sites/.*/files/styles/ {
try_files $uri #rewrite;
}
location ~* /sites/.*/files/vimages/ {
try_files $uri #rewrite;
}
# Fighting with ImageCache? This little gem is amazing.
location ~ ^/sites/.*/files/imagecache/ {
try_files $uri #rewrite;
}
}
Nginx is working against the filesystem and has no control over the name of the files when serving from a directory.
If you want case insensitive you need to do a rewrite.
i tried to add caching on my all .html files without index page, but always when i did some changes my files went to 404 not found page.
This is what i have on my default config, without any changes that i made and not worked .
server {
listen 80;
server_name site.net;
root /storage/www/site.net;
access_log /var/log/nginx/site.net.access.log;
error_log /var/log/nginx/site.net.log info;
index index.php;
error_page 404 = /404.php;
if ($host = 'www.site.net' ) {
rewrite ^/(.*)$ http://site.net/$1 permanent;
}
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 ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
# This matters if you use drush
location = /backup {
deny all;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 127.0.0.1;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ #rewrite;
expires max;
}
location ~ ^/sites/.*/private/ {
access_log off;
internal;
}
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*).html$ /index.php?s=$1;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}}
Try this:
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*).html$ /index.php?s=$1 last;
}
I have a debian machine on which I installed nginx for my web server worker process. But we just need to change a little for wordpress in default nginx configuration which is located /etc/nginx/sites-enabled/ !
/etc/nginx/sites-enabled/wordpress.com
/etc/nginx/sites-enabled/drupal.com
Can I know sample nginx configurations for those two websites under single debian machine.
You can run multiple sites using nginx, analogous to an apache vhost.
In /etc/nginx/sites-enabled, you can add two vhosts
wordpress.example.com
server {
listen 80;
server_name wordpress.example.com;
root /var/www/wordpress;
# if ($http_host != "www.example.com") {
# rewrite ^ http://www.example.com$request_uri permanent;
# }
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
and
drupal.example.com
server {
listen 80;
server_name drupal.example.com;
root /var/www/drupal;
# if ($http_host != "www.example.com") {
# rewrite ^ http://www.example.com$request_uri permanent;
# }
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9001;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}