Nginx wrong root directory - nginx

I have the following server settings, and for some reason, my document root is going to the wrong location. Why is it doing that? $1 is correct on the return line, but why is it pulling the wrong root?
This block is broken:
server{
listen 80;
#server_name mission13.io www.mission13.io;
server_name "~^www\.(.*)$";
return 301 $scheme://$1$request_uri;
root /usr/share/nginx/html/$1;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
include /usr/share/nginx/conf/mission13.io.conf;
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;
}
}
This block works:
server{
listen 80;
root /usr/share/nginx/html/diskise.com;
index index.php index.html index.htm;
server_name diskise.com www.diskise.com;
location / {
try_files $uri $uri/ /index.html;
}
include /usr/share/nginx/conf/diskise.com.conf;
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;
}
}
it is using a root that is set somewhere else, a completely different file.

Related

Multiple Domain in single VPS using VHOST

I want to create multiple sites in one vps, example sites on port 80 use www.domain1.com and on port 8080 use www.domain2.com. can nginx or anything handle that?
i have tried to use port forwarding, but is not working well
my nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name domain1.com;
root /var/www/html;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# PHP
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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
and my virtualhost.conf
#Virual Host
server {
listen 8080;
access_log /var/log/nginx/nginx-access.log;
error_log /var/log/nginx/nginx-error.log;
# Root
root /var/www/web2/public;
index index.php index.html index.htm;
# server name
server_name domain2.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP
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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
if possible, how the way?
You can run both domains on port 80. you don't need to change port to 8080.
In nginx.conf change server_name from domain1.com back to default_server;
In your /etc/nginx/conf.d/ directory create two config files, domain1-com.conf and domain2-com.conf
donain1-com.conf
server {
listen 80;
server_name domain1.com www.domain1.com;
access_log /var/log/nginx/domain1-com-access.log;
error_log /var/log/nginx/domain1-com-error.log;
root /var/www/web1/public;
index index.php index.html index.htm;
# PHP
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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
domain2-com.conf
server {
listen 80;
server_name domain2.com www.domain2.com;
access_log /var/log/nginx/domain2-com-access.log;
error_log /var/log/nginx/domain2-com-error.log;
root /var/www/web2/public;
index index.php index.html index.htm;
# PHP
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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Working with alias inside location

So here's my server block
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld/html;
index index.php index.html index.htm;
location / {
}
location /phpmyadmin {
alias /var/www/phpmyadmin;
}
location /nginx_status {
stub_status on;
access_log off;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
browsing http://domain.tld/index.php works fine the only problem im encountering is browsing http://domain.tld/phpmyadmin/. It returns 404 yet the folder /var/www/phpmyadmin exist on the server. Viewing /var/log/nginx/error.log, no error is being logged there yet the access to it is logged in /var/log/nginx/access.log. What could be the problem here?
The problem is that phpmyadmin is a PHP application and your location ~ \.php$ block does not point to the correct document root.
You need to construct two PHP locations with different document roots.
If phpmyadmin is located at /var/www/phpmyadmin, you do not need an alias directive, as a root directive will be more efficient. See this document.
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld/html;
index index.php index.html index.htm;
location / {
}
location /nginx_status {
stub_status on;
access_log off;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ^~ /phpmyadmin {
root /var/www;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
The location ^~ /phpmyadmin is a prefix location that takes precedence over the regex location normally used to process .php files. It contains a location ~ \.php$ block which inherits a value of /var/www for the document root.
It is advisable to include fastcgi_params before defining other fastcgi_param parameters otherwise your custom values may be silently overwritten.
See this document for more.

Nginx Wild Card Domains

I have an A Name domain setup on Nginx, I also have wildcard DNS setup on Digital Ocean.
I would like my main domain to point to where it is: /usr/share/nginx/html/mission13.io
I would then like all the wildcard domains to point to their respective folders:
/usr/share/nginx/html/sub1.mission13.io
/usr/share/nginx/html/sub2.mission13.io
/usr/share/nginx/html/sub3.mission13.io
Here is what I have, but I am not sure what to do to get them to point to the proper location. What can I change to do that?
server{
listen 80;
root /usr/share/nginx/html/mission13.io;
index index.php index.html index.htm;
server_name mission13.io www.mission13.io *.mission13.io;
location / {
try_files $uri $uri/ /index.html;
}
include /usr/share/nginx/conf/mission13.io.conf;
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;
}
}
Another way please change server name to _ then it will search folder with the host name inside root /usr/share/nginx/html/ directory.so,your directory looks clean
server{
listen 80;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.html;
}
include /usr/share/nginx/conf/mission13.io.conf;
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;
}
}
Got it!
I need to use $http_host like so:
root /usr/share/nginx/html/$http_host;

nginx configuration for Laravel 4

I am trying to setup my Laravel 4 project using nginx . Here is my nginx server block for laravel :
server {
listen 80;
root /home/prism/www/laravel/public;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
But my problem is , Its showing "404 not found" error for all other routes except the default one , that comes with default installation .
This is an NGINX Configuration i've used with Laravel 4 and Laravel 4.1 that works.
server {
listen 80;
server_name sub.domain.com;
set $root_path '/var/www/html/application_name/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}
you could try this for location / { ... }
location / {
try_files $uri $uri/ /index.php?$query_string;
}
$query_string worked for me.

Laravel 4 nginx servername/myappname

I try to make my production server to serve my applications in a same style as phpmyadmin is served. Like example.com/phpmyadmin and what I try to do is example.com/myappname.
My nginx/sites-available/default looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www;
index index.html index.htm index.php;
server_name _;
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /myapp {
root /var/www/myapp/public/;
index index.php;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
location ~ /\.ht {
deny all;
}
}
So if someone has a working conf for this, I would like to have it too!
Notice that your /phpmyadmin location has extra configuration to pass the request to PHP-FPM via the fast_cgi configurations.
You'll need to do that for your /myapp location as well.
You may want to change your myapp location to something like this:
location ^/myapp/(.+\.php)$ {
root /var/www/myapp/public/;
index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
For comparison, here is my Laravel nginx configuration - however I don't have my app running under a "sub-directory".
Try changing some configuration items to see what you can get to work. Note that I removed fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; from the Laravel app location - I dont believe it is necessary.
Lastly, would this make more sense on ServerFault? You may get better answers there.

Resources