I have the following default configuration file in nginx:
server {
listen 80;
root /home/d/www;
index index.php index.html index.htm;
server_name localhost;
Recently I added a file to host a specific domain, like:
server {
listen 80;
root /home/d/sites/dom
index index.html index.htm;
server_name dom.co www.dom.co;
After adding this, when loading the public server IP would be routed to this domain configuration folder, /home/d/sites/dom
How can the IP be directed to the default root?
There's listen 80 default; for requests without Host header (if none set - the first one is used)
For requests with unrecognized host - special server_name _; matches
To add to Vasfed's answer, my block are:
/etc/nginx/sites-available/yourdomain
server {
listen 80;
root /www/sites/**yourdomain**;
index index.html index.htm;
server_name **yourdomain**.com www.**yourdomain**.com;
}
/etc/nginx/sites-available/default
server {
listen 80 default_server;
root /www;
index index.php index.html index.htm;
server_name _;
}
These two files need to by sym-linked to sites-enabled.
Related
I want to add new port number to nginx server or how to add new server block to nginx server.
Create a new file in the Nginx directory /etc/nginx/sites-enabled and add a new server block here (example shown below).
server {
# Change me to change my port number
listen 8080;
server_name example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
What you probably want is multiple "server" stanzas, each with a different port. You may want to read more into the documentation here.
Example:
server {
listen 80;
server_name example.org www.example.org;
root /var/www/port80/
}
server {
listen 81;
server_name *.example.org;
root /var/www/port81/
}
You can use the same server_name, serving the "different" content appropriately within each one. If you want to have the same server_name and root, but multiple ports, another solution would be this:
server {
listen 80;
listen 81;
server_name example.org;
root /var/www/;
}
Cheers!
I have two angular apps running on different ports. I need to access app1 when the user calls https://url and app2 when the user calls https://url/admin. I tried adding location block to the Nginx config as below but the request is directed to app1 with the /admin added.
server {
# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
root /usr/share/nginx/html;
index index.html index.htm;
server_name fr.techolution.com;
ssl_certificate /etc/nginx/ssl/url_com.crt;
ssl_certificate_key /etc/nginx/ssl/myserver.key;
location / {
proxy_pass http://192.168.2.81:8083;
}
location /admin/ {
proxy_pass http://192.168.2.81:4200;
}
}
Both requests https://url and https://url/admin will be proceeds in the location:
location / {
proxy_pass http://192.168.2.81:8083;
}
Looks like you should remove slash from your second location:
location /admin {
proxy_pass http://192.168.2.81:4200;
}
If the server ip is 10.0.0.0, but I'm mapping it to make it to have www.example.com, is the below config the correct way to do it?
server {
listen 80;
server_name 10.0.0.0 example.com;
access_log /var/log/nginx/example.log;
No, if you want to restrict a server to a single interface address, it needs to go with the listen directive:
server {
listen 10.0.0.0:80;
server_name example.com;
access_log /var/log/nginx/example.log;
...
}
above answer, or you can just setup a basic server block
server {
listen 80;
listen [::]:80;
root /var/www/folderName/htdocs; // where you have your project folder and public directory
index index.html index.htm; // add index.php here if using php files
server_name test.com www.test.com; // desired url goes here
location / {
try_files $uri $uri/ =404;
}
}
the tutorials from digital ocean are pretty nice - https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts
I've changed my conf file so that when a user types in the domain without www it redirects to the domain with www:
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
I also wish for my https for anything under /user
I get the error of too may redirects, where am I going wrong?
So I have:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/example.com/site;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com;
return 301 $scheme://www.example.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/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /user {
rewrite ^ https://$http_host$request_uri? permanent;
}
}
For port 443:
server {
listen 443;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
root /var/www/example.com/site;
index index.html index.htm;
ssl on;
ssl_certificate //path here
ssl_certificate_key //path here
location / {
rewrite ^ http://$http_host$request_uri? permanent;
}
location /user {
}
}
With
listen 80 default_server;
you are telling nginx that this server block is the default server for all http requests regardless of the server name.
The directive
return 301 $scheme://www.example.com$request_uri;
sets nginx to redirect all incoming traffic for this server block to www.example.com. That redirected traffic hits the same server block again (default server) and the process repeats itself, hence a redirect loop.
To fix this change your config file and add a second server block:
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name www.example.com;
#rest of your config
[...]
}
server {
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
Port 80 can be left out, as it is the default, in case you are wondering.
The same principle applies to traffic for 443 (in the redirect block you however must give the port and ssl specific configuration).
I'm trying to redirect my domain http://example.com to http://www.example.com on Digital Ocean.
I have A records for both www and #, so the site is accessible at each domain, but the redirect is not working. Here is what I have in my default config file (/etc/nginx/sites-enabled/default):
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/app/deploy/frontend/;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
#Redirect example.com to www.example.com
server {
listen 80;
server_name example.com;
return 301 http://www.example.com.io$request_uri;
}
Any ideas?
The correct way is to define them in separate server blocks. Can be within the same file though like this:
server {
server_name domain.com;
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
server_name www.domain.com;
#The rest of your configuration goes here#
}
Create a CNAME record with 'yoursite.com' is an alias of '#'