Main domain keeps going to a subdomain - nginx

I have a Wordpress site in abc.com, and a Rails app in app.xyz.com, same server. Take note of different domain. But when I type abc.com, it shows content in app.xyz.com, and in the address bar shows abc.com. What have I done wrong?
nginx config file for abc.com
server {
listen 80;
root /var/www/abc;
index index.php index.html index.htm index.nginx-debian.html;
server_name abc.com;
client_max_body_size 20M;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~ ^/\.user\.ini {
deny all;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
}
nginx config file for app.xyz.com
upstream xyz {
server unix:/home/deployer/app/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name app.xyz.com;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 503 504 /500;
root /home/deployer/app/current/public;
try_files $uri/index.html $uri.html $uri #app;
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://xyz;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}

In nginx config file for abc.com, add this on top:
server {
server_name abc.com;
return 301 $scheme://www.abc.com$request_uri;
}

Related

.well-known/acme-challenge/xxxx gives 404 nginx

I manage a wordpress site.
I have below code block in my nginx config file.
server {
listen 443 ssl;
server_name example.com;
client_max_body_size 1000M;
root /var/www/wordpress;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
proxy_read_timeout 3000;
proxy_connect_timeout 3000;
proxy_send_timeout 3000;
expires 1s;
set $skip_cache 0;
# POST requests and URLs with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
# Don't cache URIs containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php
|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don't use the cache for logged-in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass
|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
location / {
autoindex on;
client_max_body_size 1000M;
try_files $uri $uri/ /index.php$is_args$args;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires 1s;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 60m;
fastcgi_cache_lock on;
expires 1s;
add_header X-FastCGI-Cache $upstream_cache_status;
}
location ~ /.well-known {
allow all;
}
location ^~ /.well-known/acme-challenge/ {
allow all;
default_type "text/plain";
try_files $uri =404;
}
}
I created test file in /var/www/wordpress/.well-known/acme-challenge/ folder.
when I load https://example.com/.well-known/acme-challenge it gives me nice 404 from wordpress.
when I load https://example.com/.well-known/acme-challenge/ it gives me below error
404 Not Found
nginx/1.14.0 (Ubuntu)
when I try to load https://example.com/.well-known/acme-challenge/test also it gives me. below 404 error.
404 Not Found
nginx/1.14.0 (Ubuntu)
Because of this my ssl renew fails.
What am I missing here ?

Problem With nginx and wordpress subfolder install -- permalinks not working

Wordpress index page (default post url) working but posts are not once I switch to custom permalink
wordpress 5.0.3
nginx 1.14.0
my nginx file
server {
listen 80:
listen [::]:80;
server_name www.example.com;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /blog {
alias /var/www/wp;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
location /test {
alias /var/www/test;
try_file $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
}
my folder structure:
/var/www/html -> http://www.example.com/
/var/www/wp -> http://www.example.com/blog
Working:
http://www.example.com
http://www.example.com/test
http://www.example.com/blog/?p=123
Not working:
http://www.example.com/blog/archives/123
http://www.example.com/blog/sample-post/

Nginx 500 error page is not working with matches any unmatched URL

In my Nginx conf. error 500 rule is not working with location conf. lines below.
location / {
try_files $uri $uri/ #rewrite;
}
error_page 500 /error.html;
location = /error.html {
root /path/to/nginx;
internal;
}
location #rewrite{
rewrite ^ /index.php?q=$uri;
}
The error.html file is in /path/to/nginx directory.
When I create a 500 error in the code, it couldn't run the Nginx 500 rule.
If I remove the location / { part, and change 500 to 404, it works.
Edit:
server {
listen 80;
listen 443 ssl;
server_name_in_redirect off;
port_in_redirect off;
server_tokens off;
server_name www-dev.somc.io;
root /path/to/site;
index index.php index.html index;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
rewrite_log on;
error_page 500 /error.html;
location = /error.html {
root /usr/share/nginx/html;
internal;
}
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite{
rewrite ^ /index.php?q=$uri;
}
add_header Cache-Control no-cache;
set $skip_cache 0;
set $cache_control max-age=660;
location ~ \.php$ {
include /etc/nginx/php-handler.conf;
fastcgi_pass hhvm;
}
}

How to add OwnCloud location to an exiting nginx conf

Nginx already running and i need to add location for example.com/owncloud
It turned out that my setting causes redirect loop for the link above.
Also there is an iRedMail bundle installed and it works with its default settings
Here is part of config for example.com/owncloud
location ~ ^/owncloud { rewrite ^ https://$host/owncloud; }
location ~ ^/owncloud(.*) {
alias /var/www/owncloud/;
index index.html;
}
Here is whole conf file
upstream php_workers {
server unix:/var/run/php-fpm.socket;
}
# HTTP
server {
listen 80;
server_name _;
root /var/www;
index index.php index.html index.htm;
location / {
root /var/www;
}
# Normal PHP scripts
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php_workers;
}
# Redirect webmail/SOGo/iredadmin to HTTPS
location ~ ^/mail { rewrite ^ https://$host$request_uri?; }
location ~* ^/sogo { rewrite ^ https://$host/SOGo; }
location ~ ^/iredadmin { rewrite ^ https://$host$request_uri?; }
location ~ ^/owncloud { rewrite ^ https://$host/owncloud; }
# Deny all attempts to access hidden files such as .htaccess.
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Handling noisy favicon.ico messages
location = /favicon.ico {
access_log off;
log_not_found off;
}
}
# HTTPS
server {
listen 443;
server_name _;
ssl on;
ssl_certificate /etc/ssl/certs/iRedMail.crt;
ssl_certificate_key /etc/ssl/private/iRedMail.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.php index.html index.htm;
location / {
root /var/www;
}
# Deny all attempts to access hidden files such as .htaccess.
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Handling noisy favicon.ico messages
location = ^/favicon.ico {
access_log off;
log_not_found off;
}
# Roundcube webmail
location ~ ^/mail(.*)\.php$ {
include fastcgi_params;
fastcgi_pass php_workers;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/apache2/roundcubemail$1.php;
}
location ~ ^/mail(.*) {
alias /usr/share/apache2/roundcubemail$1;
index index.php;
}
location ~ ^/owncloud(.*) {
alias /var/www/owncloud/;
index index.html;
}
location ~ ^/mail/(bin|SQL|README|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ { deny all; }
# Normal PHP scripts
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php_workers;
}
# iRedAdmin: static files under /iredadmin/static
location ~ ^/iredadmin/static/(.*)\.(png|jpg|gif|css|js) {
alias /usr/share/apache2/iredadmin/static/$1.$2;
}
# iRedAdmin: Python scripts
location ~ ^/mail(.*) {
alias /usr/share/apache2/roundcubemail$1;
index index.php;
}
location ~ ^/owncloud(.*) {
alias /var/www/owncloud/;
index index.html;
}
location ~ ^/mail/(bin|SQL|README|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ { deny all; }
# Normal PHP scripts
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php_workers;
}
# iRedAdmin: static files under /iredadmin/static
location ~ ^/iredadmin/static/(.*)\.(png|jpg|gif|css|js) {
alias /usr/share/apache2/iredadmin/static/$1.$2;
}
# iRedAdmin: Python scripts
location ~ ^/iredadmin(.*) {
rewrite ^/iredadmin(/.*)$ $1 break;
include uwsgi_params;
uwsgi_pass unix:/var/run/uwsgi_iredadmin.socket;
uwsgi_param UWSGI_CHDIR /usr/share/apache2/iredadmin;
uwsgi_param UWSGI_SCRIPT iredadmin;
uwsgi_param SCRIPT_NAME /iredadmin;
}
# iRedAdmin: redirect /iredadmin to /iredadmin/
location = /iredadmin {
rewrite ^ /iredadmin/;
}
# SOGo
location ~ ^/sogo { rewrite ^ https://$host/SOGo; }
location ~ ^/SOGO { rewrite ^ https://$host/SOGo; }
# For IOS 7
location = /principals/ {
rewrite ^ https://$server_name/SOGo/dav;
allow all;
}
location ^~ /SOGo {
proxy_pass http://127.0.0.1:20000;
#proxy_redirect http://127.0.0.1:20000/SOGo/ /SOGo;
rewrite ^/iredadmin(/.*)$ $1 break;
include uwsgi_params;
uwsgi_pass unix:/var/run/uwsgi_iredadmin.socket;
uwsgi_param UWSGI_CHDIR /usr/share/apache2/iredadmin;
uwsgi_param UWSGI_SCRIPT iredadmin;
uwsgi_param SCRIPT_NAME /iredadmin;
}
# iRedAdmin: redirect /iredadmin to /iredadmin/
location = /iredadmin {
rewrite ^ /iredadmin/;
}
# SOGo
location ~ ^/sogo { rewrite ^ https://$host/SOGo; }
location ~ ^/SOGO { rewrite ^ https://$host/SOGo; }
# For IOS 7
location = /principals/ {
rewrite ^ https://$server_name/SOGo/dav;
allow all;
}
location ^~ /SOGo {
proxy_pass http://127.0.0.1:20000;
#proxy_redirect http://127.0.0.1:20000/SOGo/ /SOGo;
# forward user's IP address
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $host;
proxy_set_header x-webobjects-server-protocol HTTP/1.0;
#proxy_set_header x-webobjects-remote-host 127.0.0.1;
#proxy_set_header x-webobjects-server-name $server_name;
#proxy_set_header x-webobjects-server-url $scheme://$host;
}
location ^~ /Microsoft-Server-ActiveSync {
proxy_pass http://127.0.0.1:20000/SOGo/Microsoft-Server-ActiveSync;
proxy_redirect http://127.0.0.1:20000/Microsoft-Server-ActiveSync /;
}
location ^~ /SOGo/Microsoft-Server-ActiveSync {
proxy_pass http://127.0.0.1:20000/SOGo/Microsoft-Server-ActiveSync;
proxy_redirect http://127.0.0.1:20000/SOGo/Microsoft-Server-ActiveSync /;
}
location /SOGo.woa/WebServerResources/ {
alias /usr/lib/GNUstep/SOGo/WebServerResources/;
}
location /SOGo/WebServerResources/ {
alias /usr/lib/GNUstep/SOGo/WebServerResources/;
}
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $host;
proxy_set_header x-webobjects-server-protocol HTTP/1.0;
#proxy_set_header x-webobjects-remote-host 127.0.0.1;
#proxy_set_header x-webobjects-server-name $server_name;
#proxy_set_header x-webobjects-server-url $scheme://$host;
}
location ^~ /Microsoft-Server-ActiveSync {
proxy_pass http://127.0.0.1:20000/SOGo/Microsoft-Server-ActiveSync;
proxy_redirect http://127.0.0.1:20000/Microsoft-Server-ActiveSync /;
}
location ^~ /SOGo/Microsoft-Server-ActiveSync {
proxy_pass http://127.0.0.1:20000/SOGo/Microsoft-Server-ActiveSync;
proxy_redirect http://127.0.0.1:20000/SOGo/Microsoft-Server-ActiveSync /;
}
location /SOGo.woa/WebServerResources/ {
alias /usr/lib/GNUstep/SOGo/WebServerResources/;
}
location /SOGo/WebServerResources/ {
alias /usr/lib/GNUstep/SOGo/WebServerResources/;
}
location ^/SOGo/so/ControlPanel/Products/([^/]*)/Resources/(.*)$ {
alias /usr/lib/GNUstep/SOGo/$1.SOGo/Resources/$2;
}
}

Nginx returns 404 on cached html files

i tried to add caching on my all .html files without index page, but always when i did some changes my files went to 404 not found page.
This is what i have on my default config, without any changes that i made and not worked .
server {
listen 80;
server_name site.net;
root /storage/www/site.net;
access_log /var/log/nginx/site.net.access.log;
error_log /var/log/nginx/site.net.log info;
index index.php;
error_page 404 = /404.php;
if ($host = 'www.site.net' ) {
rewrite ^/(.*)$ http://site.net/$1 permanent;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
# This matters if you use drush
location = /backup {
deny all;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 127.0.0.1;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ #rewrite;
expires max;
}
location ~ ^/sites/.*/private/ {
access_log off;
internal;
}
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*).html$ /index.php?s=$1;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}}
Try this:
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*).html$ /index.php?s=$1 last;
}

Resources