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;
}
}
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.
I tried everything I could, I can not make it work.
I'd like to redirect my subdomains to a specific folder in my Debian server using NGinx, here's the configurations I tried :
server {
listen 8080;
server_name ~^(?<user>.+)\.example\.net$;
root /srv/www/example.net/$user;
}
=> error is :
Starting nginx: [emerg]: unknown "user" variable configuration file
/etc/nginx/nginx.conf test failed
(note: I also tried without the ^ as indicated here : Nginx server_name regexp not working as variable)
If I try this instead :
server {
listen 8080;
server_name *.example.net$;
root /srv/www/example.net/$1;
}
Error is on the request :
2013/08/20 15:38:42 [error] 5456#0: *6 directory index of
"/srv/www/example.net//" is forbidden, client: xxx.xxx.xxx.xxx, server:
*.example.net, request: "GET / HTTP/1.1", host: "test.example.net:8080"
Aka, $1 is empty !
The documentation is wrong then :
http://nginx.org/en/docs/http/server_names.html
Update:
This is working (taken from https://serverfault.com/questions/457196/dynamic-nginx-domain-root-path-based-on-hostname):
server {
server_name ~^(.+)\.example\.com$;
root /var/www/example.com/$1/;
}
BUT I'd like to display PHP Pages, and if I add the following in my server {}, the $1 is then empty (wtf?) :
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;
}
# Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
deny all;
}
# 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 ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
server_tokens off;
try_files $uri $uri/ /index.php?$args;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_intercept_errors off;
fastcgi_send_timeout 30s;
fastcgi_read_timeout 30s;
}
I finally found the solution, and it's not so pretty.
In fact it was a mix of an old NGinx version (0.7.67, in Debian Squeeze) and some odd reaction (maybe from this version) of the NGinx configuration.
The following code works fine, but successful only in a NGinx version of 1.2.1 (it fails in 0.7.67, and not tested in other versions) :
map $host $username {
~^(?P<user>.+)\.example\.com$ $user;
}
server {
listen 80;
server_name *.example.com;
root /var/www/example.com/$username;
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;
}
# Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
deny all;
}
# 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 ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
server_tokens off;
try_files $uri $uri/ /index.php?$args;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_intercept_errors off;
fastcgi_send_timeout 30s;
fastcgi_read_timeout 30s;
}
}
This alternative also works (for newer PCRE versions) :
map $host $username {
~^(?<user>.+)\.example\.com$ $user;
}
I had to combine all found solutions to create my own working .conf This is my answer from similar question https://stackoverflow.com/a/40113553/1713660
server {
listen 80;
server_name ~^(?P<sub>.+)\.example\.com$;
root /var/www/$sub;
location / {
index index.php index.html;
}
}
The correct form is:
server {
listen 8080;
server_name ~^(?P<user>.+)\.example\.net$;
location / {
root /srv/www/example.net/$user;
}
}
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;
}
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.