Setting up multiple domains on nginx server - nginx

I have one nginx server on ubuntu running a single application. I want let multiple domains point to this server.
Currently my /etc/nginx/sites-available/default looks like this:
(website1.com is just an example as I don't want to give out my real domain)
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/website/public;
index index.php index.html index.htm;
server_name website1.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This works when someone going to website1.com
Now if people coming from website2.com do i just copy the code again exactly and change the server_name?
Or is there anything else I must do?

You can just list as many domain names on your server name directive
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/website/public;
index index.php index.html index.htm;
server_name website1.com website2.com website3.com;
...
Bear in mind that you would need a dns that resolves website2.com to the same ip as website2.com also!

Related

Nginx location rules not applying

I want to run both WordPress and YOURLS on one domain which is configured by a NGINX server block (not the default site). Since both need to handle URLs differently, they need different try_files directives. WordPress sits on the root of the domain (domain.tld), while YOURLS is being installed to the /g/ directory. Despite the two location rules, I get 404s on any links generated by YOURLS (e.g. domain.tld/g/linkname, all are redirects to external URLs), though I can access the admin backend.
As far as I read, declaring to location rules (one for /g/, and one for /) should suffice in order to let NGINX handle the direct and the /g/ URLS differently - is there something in wrong in my thinking?
The try_files rules are correct and do work well on other single-application server block (WordPress as well as YOURLS on installs on separate server blocks).
The server block definition config looks like this:
server {
listen [::]:80;
listen 80;
server_name domain.tld www.domain.tld;
return 301 https://domain.tld$request_uri;
}
server {
listen [::]:443 ssl;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
root /var/www/html/domain.tld;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name domain.tld www.domain.tld;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
}
location /g/ {
try_files $uri $uri/ /yourls-loader.php$is_args$args;
expires 14d;
add_header Cache-Control 'public';
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
The problem with the location /g/ try_files directive is that the path to the YOURLS loader isn't correct. If the URL handler (yourls-loader.php) lies within the /g directory, the path to it has to be changed to include the /g directory:
try_files $uri $uri/ /g/yourls-loader.php$is_args$args;
The location rule does not imply that each path is handled from that location as well, but rather from the root path given above.

Deployment Laravel 5.4 - DigitalOcean - Nginx LEMP (16.04)

i'm deployment my laravel project in digitalocean, actually i have this situation:
DNS RECORDS - GODADDY
digitalocean ip server 104.131.16.82
DNS RECORDS DIGITALOCEAN
SERVER BLOCK ENABLED
i enabled my default server block in "/etc/nginx/sites-available/default" and created link in "/etc/nginx/sites-enabled/default" like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/mysite-test.com/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
return 301 $scheme://mysite-test.com$request_uri;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php7-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
i followed this tutorial to install: https://gist.github.com/naveenyagati/5deec8fc40a2faaff20c629362dddf39
Actually if i go to 104.131.16.82 download a file, i don't know why, it doesn't work well, maybe i have error in server block? or DNS records?
thank you for your help!
If you have deployed using ONE CLICK APP INSTALL in Digital Ocean you have to edit the file named "digitalocean" in "/etc/nginx/site-available" directory not the "default" file for changes to take effect. If not the default LEMP landing page would be shown there.
Command to edit the main file,in digital ocean ONE CLICK APP INSTALL
sudo nano /etc/nginx/sites-available/digitalocean
Now replace the contents of the file with the following code
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name <Your Domain name / Public IP Address>;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Replace the server_name part with your
server IP or domain name
Further reference could be found in the following gist that I have made
https://gist.github.com/naveenyagati/5deec8fc40a2faaff20c629362dddf39

nginx multiple domain issue

I want to host two website on my ubuntu VPS with nginx . so i created two copy of default config :domain1.com and domain2.com
config for domain1.com:
server {
listen 80 ;
listen [::]:80 ;
root /home/sher/ftp/www/public;
server_name domain1.com www.domain1.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
if user type in www.domain1.com in browser he would see domain1 site. but if he hit it without www he would see domain2.com .i've created symlinks from these two config files to sites-enabled .
P.S : I set listen to default_server for domain1 and it seems the issue solved but im not sure if it is the right way.

NGINX doesn't work if the domain has an accent mark

I have bought a domain with an accent mark inside: something like www.èxample.com.
My problem is that even if I configured my nginx server block (virtual host) in a correct way, it doesn't match the directory that I made.
I tried to use the same configuration with a domain without an accent mark and everything work fine.
This is my server block file inside site-availables folder:
server {
listen 80;
root /var/www/example.com;
index index.php index.html index.html;
server_name *.èxample.com;
client_max_body_size 4G;
charset utf-8;
access_log /var/www/example.com/logs/nginx-access.log;
error_log /var/www/example.com/logs/nginx-error.log;
location / {
try_files $uri $uri/ /index.html;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
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;
}
}
How can I solve this problem?
Thanks!

Yourls Errors in WordPress

I have wordpress working well in mysite.com
but YOURLS which is installed in mysite.com/u is not working, when I click on any shortened link I get a 404 error (wordpress).
However, I get YOURLS to work by adding this to nginx.conf
location /u { try_files $uri $uri/ /u/yourls-loader.php;
But then WordPress links break.
Here is my default nginx.conf
I know the fix is to add this try_files $uri $uri/ /u/yourls-loader.php; somewhere in nginx.conf , but where to put it without breaking wordpress.?
=================== Update 1 =========================
I got this partially working. with same config, but I noticed that wordpress links that start with u doesn't work ex: http://example.com/understand-math instead it redirect to Error 403 - Forbidden
???
================ update 2 ============
ok I fixed it by just adding another slash / to location /u/ instead of location /u
YOURLs NGINX CONFIGURATION
server {
# Listen IPv4 & v6
listen 80;
listen [::]:80;
# Optional SSL stuff
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate example.com.crt;
ssl_certificate_key example.com.key;
# Server names
server_name example.com www.example.com;
# Root directory (NEEDS CONFIGURATION)
root /path/to/files;
# Rewrites
location / {
# Try files, then folders, then yourls-loader.php
# --- The most important line ---
try_files $uri $uri/ /yourls-loader.php;
# PHP engine
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock; # Can be different
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
GITHUB DOCUMENTATION

Resources