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;
Related
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.
My domain registrar's DNS hits my server and gets the nginx default page, so that's properly configured
I copied an nginx vhost that is currently working, changed the server_name and the name of the conf file and nothing else.
Diff:
diff 701sm.club.conf drz400.info.conf
3c3
< server_name www.701sm.club 701sm.club;
---
> server_name www.drz400.info drz400.info;
then I restarted nginx.
Here is the entire non-functioning vhost:
server {
listen 80;
server_name www.701sm.club 701sm.club;
index index.php index.html index.htm;
access_log /var/www/drz400.info/logs/access.log;
error_log /var/www/drz400.info/logs/error.log;
location / {
root /var/www/drz400.info/production;
}
location ~ \.php$ {
root /var/www/drz400.info/production;
try_files $uri =404;
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;
}
}
Both sites should hit my .../production/index.html, but only one does. What could be wrong here?
Please try the following code,
server {
listen 80;
server_name www.701sm.club 701sm.club;
index index.php index.html index.htm;
access_log /var/www/drz400.info/logs/access.log;
error_log /var/www/drz400.info/logs/error.log;
root /var/www/drz400.info/production;
location / {
try_files $uri /index.html;
}
location ~ \.php$ {
try_files $uri =404;
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;
}
}
Just got curious about your log directive, you point out the log to /var/www/drz400.info/logs/error.log while your domain was www.701sm.club. If you access www.701sm.club, is there any error on your error.log ?
And what type of error was occurred ?
I am really new to Nginx, and am to add a new website to a remote Nginx server.
I notice that /etc/nginx/conf.d/default.conf has been modified to this:
server {
server_name api.example.com;
root /var/www/html/api/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.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;
}
}
I want to add another url: testapi.example.com to the same server. All the tuturiols point to /etc/nginx/sites-enabled/, but that folder is empty. This is a production machine, so I would not like bring any downtime.
Please help.
You could add another server block to /etc/nginx/conf.d/default.conf for your second domain or a better way would be to create a new .conf file in /etc/nginx/conf.d/ (with a meaningful name) and in that file define a new server block for the second domain.
So you'd keep your /etc/nginx/conf.d/default.conf
server {
server_name api.example.com;
root /var/www/html/api/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.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 add /etc/nginx/conf.d/testapi.conf
server {
server_name testapi.example.com;
root /var/www/html/testapi/root;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
... rest of config for second domain...
}
I am trying to use different directory from where to serve the files, but have been unsuccessful. /var/www/site.app/public is where the root, but I want to serve news from /var/www/news/api instead, while having URL http://site.app/news/123, from which I want to remove the news part, because otherwise it would map to /var/www/news/api/news/123.
Judging by the debug logs, it seems that it gets rewritten correctly when first testing all location blocks, but after it is done rewritting, it goes through all of them again, and ends up serving content with location block /.
Here is my configuration file I have.
server {
listen 80;
server_name site.app;
root /var/www/site.app/public;
rewrite_log on;
error_log /var/log/nginx/error.log debug;
index index.php index.html;
location / {
try_files $uri $uri/ index.php?$query_string;
}
location /news/ {
root /var/www/news/api;
rewrite ^/news/(.*) /api.php?_=$1;
index index.php index.html;
}
location ~ \.(hh|php)$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The root directive in your /news/ location currently does nothing. You are rewriting the URI to /api.php which then hits your \.(hh|php)$ location, which inherits the root from the parent server container.
You need a distinct location for /api.php, possibly /news/api.php. In which case you can construct a separate location block (possibly nested within /news/) where you can place fastcgi_pass code using the different root.
So this is not ideal solution, but it works. By default it would still allow access to api.php, but I am denying access to it by checking if the URL contains /news/
server {
listen 80;
server_name site.app;
root /var/www/site.app/public;
index index.php index.html;
location / {
try_files $uri $uri/ index.php?$query_string;
}
location = /api.php {
if ($request_uri !~ ^/news/) {
return 444;
}
root /var/www/site.app/news/;
try_files $uri $uri/ /api.php?$query_string =408;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /news/ {
rewrite ^/news/(.*) /api.php?$1;
}
location ~ \.(hh|php)$ {
try_files $uri /index.php =407;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
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.