Nginx Multiple Domains Same IP Not Working - nginx

I am having issues with Nginx not working with multiple domains pointing to the same IP address. When accessing either domain, they both default to "catacombsfellowship.org.cnf". When accessing edmunddesoto.com, it acts as if it is catacombsfellowship.org and I get an SSL cert error where the cert is for catacombsfellowship.org.
When I run either domain by themselves, they work fine. However, they don't work seamlessly together. What am I doing wrong?
Examples:
File: edmunddesoto.com.cnf
server {
listen 80;
listen [::]:80;
server_name edmunddesoto.com default_server;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name edmunddesoto.com default_server;
root /var/www/html/edmunddesoto.com/live;
index index.php index.html index.htm index.nginx-debian.html;
ssl_certificate /etc/ssl/certs/edmunddesoto.com.crt;
ssl_certificate_key /etc/ssl/private/edmunddesoto.com.key;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
File: catacombsfellowship.org.cnf
server {
listen 80;
listen [::]:80;
server_name catacombsfellowship.org default_server;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name catacombsfellowship.org default_server;
root /var/www/html/catacombsfellowship.org/live;
index index.php index.html index.htm index.nginx-debian.html;
ssl_certificate /etc/ssl/certs/catacombsfellowship.org.crt;
ssl_certificate_key /etc/ssl/private/catacombsfellowship.org.key;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

Ok, I figured it out. The issue was somehow it was redirecting to the www.* domains and those were not set in the server_name directive. So, by adding the www.* to the server_name directive, it fixed it.

Related

HTTPS redirect www to non www

I got 2 domains and its supposed to work from same directory. While redirecting from http with/without www works perfectly, https www to non www don't work. Here is my config file:
server {
listen 80;
server_name domain1.com www.domain1.com;
return 301 https://domain1.com$request_uri;
}
server {
listen 80;
server_name domain2.com www.domain2.com;
return 301 https://domain2.com$request_uri;
}
server {
listen 443 ssl;
server_name www.domain1.com;
return 301 https://domain1.com$request_uri;
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem;
}
server {
listen 443 ssl;
server_name www.domain2.com;
return 301 https://domain2.com$request_uri;
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem;
}
server {
listen 443 ssl;
server_name domain1.com;
root /var/www/domain1.com;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
server {
listen 443 ssl;
server_name domain2.com;
root /var/www/domain1.com;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
Can you tell me what could be wrong with it? SSL certificate domain1.com have got inside certificates for all domains, include with/without WWW.
In case if someone will face to same issue, removal of "ssl" abbreviate fixed it. So instead of listen 443 ssl; in 301 server blocks change to listen 443;

How to redirect https://www to non-www https version in Nginx

I need some help with my nginx config so all my http traffic goes to https and also www changes to non-www. My server is running ubuntu 18.04 and nginx.
here is my sites-available .conf file
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name cdn.example.com;
return 301 https://cdn.example.com$request_uri;
}
server {
listen 443 http2;
ssl on;
ssl_certificate /etc/letsencrypt/live/cdn.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/cdn.example.com/privkey.pem; # managed by Certbot
root /var/www/html/storage;
index index.php index.html index.htm;
server_name cdn.example.com;
location ~* \.(jpg|jpeg|png|ico|js|css|json)$ {
expires 365d;
}
location / {
aio threads;
limit_rate_after 1m;
limit_rate 3m;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/letsencrypt/live/example.com-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem; # managed by Certbot
}
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/example.com-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem; # managed by Certbot
root /var/www/html/;
index index.php index.html index.htm;
server_name example.com www.example.com;
rewrite ^/index.php/(.*) /$1 permanent;
location ~* \.(jpg|jpeg|png|ico|js|css|json)$ {
expires 365d;
}
location / {
aio threads;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
I tried so many different configuration but none of them worked for me.

HTTPS Nginx too many redirects when 2 domains pointing same server

here my conf file
server {
root /var/www/[NAME]/latest;
index index.php index.html index.htm index.nginx-debian.html;
server_name [SITENAME].com www.[SITENAME].com [ANOTHER-SITENAME].com www.[ANOTHER-SITENAME].com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/[SITENAME].com-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/[SITENAME].com-0001/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.[ANOTHER-SITENAME].com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = [ANOTHER-SITENAME].com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = www.[SITENAME].com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = [SITENAME].com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name [SITENAME].com www.[SITENAME].com [ANOTHER-SITENAME].com www.[ANOTHER-SITENAME].com;
return 404; # managed by Certbot
}
everythign works fine when i open [SITENAME].com
but when i open [ANOTHER-SITENAME].com
i get an error message
This page isn’t working [ANOTHER-SITENAME].comredirected you too many
times.
why? how to fix this?
Your config NEVER works for [ANOTHER-SITENAME].com, because http
have redirect to https and https not working because SSL cert is
only for [SITENAME].com.
If have two sites who use same files, make
seperate blocks for them - it will be easier to manage and
troubleshoot.
Make it simple, help yourself... For example:
[SITENAME].com
server
{
listen 80;
server_name www.[SITENAME].com [SITENAME].com;
return 301 https://[SITENAME].com$request_uri;
}
server
{
listen 443 ssl;
server_name www.[SITENAME];
ssl_certificate /etc/letsencrypt/live/www.[SITENAME].com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.[SITENAME].com/privkey.pem;
return 301 https://[SITENAME].com$request_uri;
}
server
{
listen 443 ssl;
server_name [SITENAME].com;
root /var/www/[NAME]/latest;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
[ANOTHER-SITENAME].com
server
{
listen 80;
server_name www.[ANOTHER-SITENAME].com [ANOTHER-SITENAME].com;
return 301 https://[ANOTHER-SITENAME].com$request_uri;
}
server
{
listen 443 ssl;
server_name www.[ANOTHER-SITENAME].com;
ssl_certificate /etc/letsencrypt/live/www.[ANOTHER-SITENAME].com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.[ANOTHER-SITENAME].com/privkey.pem;
return 301 https://[ANOTHER-SITENAME].com$request_uri;
}
server
{
listen 443 ssl;
server_name [ANOTHER-SITENAME].com;
root /var/www/[NAME]/latest;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

How to install WordPress on sub directory in ubuntu 16.4 in same vhost with AngularJS

I have vhost for the main domain www.example.com. and I want to install WordPress in a subdirectory called news (www.example.com/news/).
This is the vhost configuration file.
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/bundle_example.com.crt;
ssl_certificate_key /etc/ssl/example.com_private_key.key;
location / {
root /www/sites/Yoga-Frontend/dist/Yoga-Frontend;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /news {
root /www/sites/Yoga-Frontend/dist/Yoga-Frontend/news;
index index.php;
try_files $uri $uri/ /news/index.php?$args /news/index.php?q=$uri&$args;
access_log /var/log/nginx/blog.access.log;
error_log /var/log/nginx/blog.error.log;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
}
}
Problem: once I enter www.example.com/news/, it always redirect to www.example.com.
I found the answer
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
root /www/sites/Yoga-Frontend/dist/Yoga-Frontend;
ssl_certificate /etc/ssl/bundle_example.com.crt;
ssl_certificate_key /etc/ssl/example.com_private_key.key;
location /yoga {
index index.php index.html index.htm;
try_files $uri $uri/ /yoga/index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/yoga)(/.*)$;
include fastcgi_params;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}

Nginx HTTPS SSL redirection doesn't work in Ubuntu 18.04

I've tired to configure an Nginx server with SSL but the site is not open but with https:// it's open normally.
Here is my Nginx configuration:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
client_max_body_size 20M;
root /var/www/mysite.in/site;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
listen 443;
listen [::]:443;
server_name www.mysite.com;
#ssl on;
ssl_certificate /etc/nginx/ssl/mysite.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/main_private.key;
}
The SSL was generated in GoDaddy, I've found lots of solutions, but so far none of them are working.
How can I resolve this error?
You have to create two servers(http and https) in your config and create redirect from http to https:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.mysite.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
listen [::]:443;
server_name www.mysite.com;
client_max_body_size 20M;
root /var/www/mysite.in/site;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
ssl on;
ssl_certificate /etc/nginx/ssl/mysite.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/main_private.key;
}

Resources