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...
}
Related
how guys ,
i have server that have 3 location ,
i config my nginx like :
server {
listen 80;
listen [::]:80;
server_name www.zavie.co zavie.co 81.91.147.131;
location / {
root /home/zavie/zavieco/zavie;
index index.php coming-soon.html index.htm index.nginx-debian.html;
}
location /word {
index index.php index.html index.htm;
root /home/zavie/zavieco/word;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~ /phpmyadmin {
alias /home/zavie/zavieco/phpmyadmin;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}
location /static/ {
root /home/zavie/zavieco/zavie360/zavie360;
}
location /zavie360 {
alias /home/zavie/zavieco/zavie360;
}
}
but when i open zavie.co/word this will not open wordpress , how i can config this ?
Unless you have installed WordPress under /home/zavie/zavieco/word/word, you have used the wrong value for the root directive. See this document for details.
Your location /word block does not have the ability to serve WordPress's static assets.
Use something like this:
location ^~ /word {
root /home/zavie/zavieco;
index index.php;
try_files $uri $uri/ /word/index.php;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
See this document for details.
I'm getting a 404 error after install phpMyAdmin using Digital Ocean's guide. I have multiple domains setup on Ubuntu running nginx. There is a phpmyadmin directory within /var/www/. The only difference from the guide was the following command:
sudo ln -s /usr/share/phpmyadmin/ /var/www
Do I need to add server block possibly? Since I have multiple domains, each of them has a separate configuration file.
Sample config file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/domain1.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name MY_IP_ADDRESS;
location / {
try_files $uri $uri/ =404;
}
location /phpmyadmin {
root /var/www/;
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
try_files $uri /index.php;
}
location ~ /\.ht {
deny all;
}
}
I'm not great at server administration. Any suggestions?
Define this server block it worked for me.
A basic configuration for phpmyadmin in you case.As you have created a symbolic link in /var/www.
sudo ln -s /usr/share/phpmyadmin/ /var/www
server {
listen 80;
server_name website.in www.website.in;
autoindex on;
location /phpmyadmin {
root /var/www/;
index index.php index.html;
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;
try_files $uri $uri/ /index.php;
}
}
location / {
root /var/www/your/path;
index index.php index.html;
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;
try_files $uri $uri/ /index.php;
}
}
}
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've been struggling with a NGINX configuration. I've set up a development environment (local laptop) with a configuration supporting search engine friendly (SEF) urls, but the same configuration doesn't seem to work on my test server.
local configuration:
server {
server_name example;
root /home/arciitek/git/example/public;
client_max_body_size 500M;
location /collection/ {
try_files $uri $uri/ /collection/index.php$args;
index index.php;
}
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/arciitek/git/example/public/$fastcgi_script_name;
}
}
This works fine. Now on the test environment it looks like this:
{
server_name dev.example.com;
access_log /srv/www/dev.example.com/access.log;
error_log /srv/www/dev.example.com/error.log debug;
root /srv/www/dev.example.com/public;
location /collection/ {
try_files $uri $uri/ /collection/index.php$args;
index index.php;
}
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/dev.example.com/public$fastcgi_script_name;
}
}
On my development environment everything is fine. But on my test environment, when I call a url in my browser with prettyness added : collection/[brand]/[product]. I get the: No input file specified error. Mind you, if I call a url anding with collection/ everyting works as well..
Can anyone help me with this please? if more info is needed, please let me know..
kind regards,
Erik
After frustrating for a long time, I noticed that it was not the configuration that gave me trouble, but the link to sites-enabled...
There you go... *pads himself on the back...
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;