Nginx multiple domains doesn't work - nginx

I have a perfect configuration which is nginx, php5-fpm, apc, varnish and mariadb. Everything works flawless except;
I am hosting a single web site, since my server resources are high and available, I want to host other web sites on the same server. When I try to add different websites into nginx the service simply does not restart.
here's my configuration file when everything works:
server {
listen 8080;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name www.domain1.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
I don't want to use seperate files for different virtual hosts, I want to do everything in default file. But when I add another virtual host like below and save default file. nginx won't restart.
server {
listen 8080;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name www.domain1.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
server {
listen 8080;
root /usr/share/nginx/domain2;
index index.php index.html index.htm;
server_name www.domain2.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
Please help me resolve this issue. I think something is conflicting but don't know what.

ok I found the solution just by investigating error log.
2014/08/19 21:55:07 [emerg] 5927#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
error log tells me to increase hash bucket size..
I edited nginx.conf and set the bucket size to 32 as advised in error log, it didn't work at first, but then I set it to 64 and it worked.
just search for "bucket" in nginx.conf, uncomment it, then set to 64 (or above in some cases) it will work, unless there is another issue.

Related

Allowing access to a specific file in location block nginx config

In my nginx config I have been block all access from my IP however allowed to do that with my localhost. I would like to allow global access to one file of mine xn.php I try to do that with location ^~ /xn.php and it's not working. As well I tried location /xn.php and still fail. How should I do that? I checked a lot documentation however I stuck on it
server {
listen 127.0.0.1:80;
root /var/www/html/;
index /index.php;
server_name localhost;
location / {
deny 77.777.77.0/24;
allow 127.0.0.1;
autoindex on;
index index.php;
try_files $uri /index.html /index.php;
deny all;
}
location ^~ /xn.php {
allow all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
With your current configuration xn.php content would be send as HTTP response rather than being interpreted with PHP-FPM. Additionally, any request for PHP file won't be blocked with your deny rule since it won't match the location / { ... } block. You can try this:
server {
listen 80;
root /var/www/html/;
index /index.php;
location / {
allow 127.0.0.1;
deny all;
autoindex on;
index index.php;
try_files $uri /index.html /index.php;
}
location = /xn.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ \.php$ {
allow 127.0.0.1;
deny all;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Update
Since you are listening only on 127.0.0.1 interface this server block won't be reachable from any other host at all. Right configuration for you depends on other server blocks you have in your nginx config.
Update your nginx location config a bit
location /xn.php {
allow all;
autoindex on;
index index.php;
try_files $uri $uri/ /index.php?$args;
}

nginx multiple root config issue

I have the following nginx config for multiple root (html/web is default, html/pma is additional route):
server {
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
server_name website.com;
server_tokens off;
root /usr/share/nginx/html/web;
index index.php;
location / {
try_files $uri /index.php?$args;
}
location ^~ /pma {
root /usr/share/nginx/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
So, by default /html/web/index.php is opened, however website.com/pma opens /html/pma/, where pma is PHPMyAdmin.
The problem is:
PHPMyAdmin authentification form redirects to index.php. Therefore, when I write my credentials, it redirects me to /html/web/index.php! But should /html/pma/index.php. Even log out from PHPMyAdmin redirects to /html/web/index.php!
Could anyone suggest a better way of configuration?
I can see two errors in your configuration.
You try_files statement should end with /index.php or =404, not both! See this document for details.
The nested location ~ \.php$ block will never be consulted. The outer location ~ \.php$ block take precedence. You can solve this problem by using the ^~ modifier on the surrounding prefix location. See this document for details. For example:
location ^~ /pma {
...
location ~ \.php$ {
...
}
}
location ~ \.php$ {
...
}

How can I stop nginx from responding to non-local requests?

Here's what my nginx file looks like. However when I browse to my server using the ip, I still the the "Welcome to nginx!" page
server {
listen 127.0.0.1:9070;
root /var/www/[redacted]/public/;
index index.php index.html index.htm;
server_name [redacted];
location / {
try_files $uri $uri/ /index.php$is_args$args;
allow 127.0.0.1;
deny all;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.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;
}
}
I believe that you'll find that moving the allow/deny into the server clause will clear this up:
server {
listen 127.0.0.1:9070;
root /var/www/[redacted]/public/;
index index.php index.html index.htm;
server_name [redacted];
location / {
try_files $uri $uri/ /index.php$is_args$args;
allow 127.0.0.1;
deny all;
}
becomes:
server {
listen 127.0.0.1:9070;
root /var/www/[redacted]/public/;
index index.php index.html index.htm;
server_name [redacted];
allow 127.0.0.1;
deny all;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}

Nginx + PHP-FPM Error: This Page is Temporarily Unavailble

I am trying to setup a Nginx / PHP-FPM server on my raspberry Pi (Debian) and I am having trouble getting the php to work correctly.
Text displayed on webpage: This Page is Temporarily Unavailble
I have checked the nginx logs and there are no errors being recorded.
This is my nginx.conf:
# Pi Nginx Config v0.1 10:53 30/01/2014
# NOTE: fastcgi is NOT php5-fpm
server {
listen 1080;
# server_name mysite.org;
charset utf-8;
access_log off;
root /var/www/cms;
index index.php;
location / {
try_files $uri $uri/ /index.php?id=$uri&$args;
}
location ~* /admin/.*\.php$ {
try_files $uri /admin/index.php?id=$uri&$args; # Try the admin index page
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~* \.php$ {
try_files $uri =404; # Try any .php files in root or throw a 404
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in /etc/php5/fpm/php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
expires 2h;
}
location ~* \.(?:ico|js|gif|jpg|png)$ {
expires 14d;
}
location ~* \.(htm|css|html)$ {
expires 2d;
}
# this blocks direct access to the XML files (but sitemap.xml) - that hold all the data
location ~* \.xml$ { deny all; }
location ~* \.xml\.bak$ { deny all; }
location = /sitemap.xml { allow all; }
# this prevents hidden files (beginning with a period) from being served
location ~ /\. { deny all; }
location ^~ /uploads/ {
if ($request_uri ~* \.php$) {return 403;}
}
}
I do not have enough experience to see anything wrong with this config. My server is on port 1080 and the server root is within the cms directory.
Any help would be greatly appreciated.

issue configuring nginx to look for a file in other folder

As per http://wiki.nginx.org/HttpCoreModule
I'm trying to make the server search for a file in a specific directory.
ie: http://exemple.com/sitemap.xml would look at rootFolder/sitemaps/specificname-sitemap.xml
I added this:
location /sitemap.xml {
alias /sitemaps/specificname-sitemap.xml;
}
But it doesn't work. What am I missing?
EDIT:
My complete conf is:
server {
listen 80; ## listen for ipv4; this line is default and implied
root /var/www/exemple/public;
index index.html index.htm index.php;
server_name exemple.com;
location /sitemap.xml {
alias /sitemaps/cs-sitemap.xml;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Found the solution:
location /sitemap.xml {
try_files $uri /sitemaps/specificName-sitemap.xml;
}

Resources