nginx does not serve to non-www domain - nginx

I have the following nginx configuration, which is supposed to serve the index.html file to both www.domain.com and domain.com. While www.domain.com works, domain.com returns the default nginx welcome page.
What am I doing wrong?
server {
listen 80;
listen [::]:80;
server_name domain.com www.domain.com;
root /home/ubuntu/coming-soon-page/frontend/react_ui/build;
location = /favicon.ico {
access_log off; log_not_found off;
}
error_page 403 /home/ubuntu/coming-soon-page/frontend/react_ui/build/index.html;
location / {
root /home/ubuntu/coming-soon-page/frontend/react_ui/build/;
index index.html;
try_files $uri $uri/ /index.html;
}
location = /submit_user_feedback {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/coming-soon-page/backend/api/api.sock;
}
}

Related

How to make the wp-admin access from a different subdomain?

------- Technologies I'm using ---------
Debian 10
Nginx server
PHP8.0
--------- The scenario ------------
I have exmaple.com which has an HTML static website
and I have a subdomain, blog.example.com I installed WP on it and it's working fine
but I want to make the wp-admin access on login.blog.example.com.
-------- What I tried -------------
1- I tried redirecting any /wp-* URL to login.blog.example.com, but that isn't useful if there are no files/folders of wp-admin on login.blog.example.com.
2- I followed this, but it wasn't handy since they are redirecting to 404 and a static page
https://403.ie/how-to-serve-wp-admin-from-a-separate-subdomain/
-------- The nginx configuration -----------
example.com:
server{
listen 80;
listen [::]:80;
server_name IP_ADDRESS;
return 301 http://example.com;
}
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
blog.example.com:
server {
listen 80;
listen [::]:80;
server_name blog.example.com;
root /var/www/blog.example.com;
index index.html index.php index.htm;
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;
}
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
# location /wp-admin{
# rewrite ^/wp-(.*)$ http://login.blog.example.com redirect;
# }
location ~ \.php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The first step is to create a server to handle requests to login.blog.example.com. This is identical to blog.example.com except for the server_name and any "login" bits you want to add.
Then add a redirect in the blog.example.com server block:
location ^~ /wp-admin {
return 302 $scheme://login.blog.example.com$request_uri;
}
WordPress may insist on redirecting you back to blog.example.com. This is because of the settings for site URL in the Dashboard configuration or the wp-config.php configuration file.
You should be able to drop the hostname from the HOME and SITEURL values and use / instead of http://blog.example.com.

Config nginx for Vue.js distribute project

I use the nginx for Vue.js dist server.
my Nginx config is bellow:
server {
listen 80;
server_name qy.abc.xyz;
charset utf-8;
access_log /var/log/nginx/qy.abc.xyz.access.log main;
location / {
access_log /data/nginx_log/access.log main;
error_log /data/nginx_log/error.log error;
root /data/ldl/repo/vue_user_site/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
...
I can access the qy.abc.xyz in browser success, but I can you know in my Vue.js there are many routes, if I access qy.abc.xyz/index or qy.abc.xyz/xxx/xxx, Nginx will get the 404 Not Found error.
You know the dist directory is constituted by many hashed-name.js and a index.html.
How to config my Nginx for my project?
EDIT-1
I tried use this config, but not work.
location / {
access_log /data/nginx_log/access.log main;
error_log /data/nginx_log/error.log error;
#root /data/ldl/repo/vue_user_site/dist;
#index index.html;
#try_files $uri $uri/ /index.html;
return 200 /index.html;
}
I need to configurate URL Rewrite
you can try this
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root <your static file root>
}
location / {
return 200 /index.html
# or use rewrite
rewrite ^.*$ /index.html
}
Or use some server code that has route like node , asp.net core to send your html
Follow my settings of single-page application config in Nginx:
server {
listen 80;
server_name qy.abc.xyz;
#access_log /var/log/logs/qy.abc.xyz.access.log main;
charset utf-8;
index index.html;
root /data/ldl/repo/vue_user_site/dist;
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}

Nginx configuration causing too many redirects

I have a conf file with example.com as the root. In the example.com directory, there is an html, css, img, and js folder. I understand this deviates from the traditional html directory as root. I have tried many different configurations (using regex based on filetypes, variables, etc.) but I always get too many redirect errors. Can anyone help on a good conf file for this type of directory structure? Here is my conf file currently.
server {
listen 80 default_server;
listen [::]:80 default_server;
# return 301 https://$server_name$request_uri;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com example.com;
root /var/www/www.example.com/;
index index.php index.html;
client_max_body_size 100m;
error_page 404 = error.html?error=404;
location ~ /.well-known {
allow all;
}
location / {
location ~* \.(html|php)$ {
root html/;
}
location ~* \.css$ {
root css/;
}
location ~* \.js$ {
root js/;
}
location ~* \.(png|jpeg|gif)$ {
root img/;
}
try_files $uri =404;
}
}
Thanks in advance for any help!
Here is the configuration I ended up using:
server {
listen 80 default_server;
listen [::]:80 default_server;
# return 301 https://$server_name$request_uri;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com example.com;
root /var/www/www.example.com/;
index index.php index.html;
client_max_body_size 100m;
error_page 404 = /html/error.html?error=404;
location ~ /.well-known {
allow all;
}
location = / {
try_files /html/index.html =404;
}
location / {
location ~* \.(html|php)$ {
try_files $uri /html/$uri =404;
}
location ~* \.css$ {
try_files $uri /css/$uri =404;
}
location ~* \.js$ {
try_files $uri /js/$uri =404;
}
location ~* \.(png|jpeg|gif)$ {
try_files $uri /img/$uri =404;
}
try_files $uri =404;
}
}
My problem was that all of my redirects were using relative pathing (such as try_files html/$uri) instead of absolute pathing from the site root (/html/$uri). This lead to redirects like /html/html/html/...
I thought that if I used an absolute path, it would be absolute to the root of the server, and not the site.
My only issue now is that my parameter on my error page redirect (?error=404) doesn't work with absolute pathing, but that not a huge deal.

Host multipe sites on nginx

Currently my nginx.conf looks like this:
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/html;
index index.html index.htm;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# Attempt to load static files, if not found route to #rootfiles
location ~ (.+)\.(html|json|txt|js|css|jpg|jpeg|gif|png|svg|ico|eot|otf|woff|woff2|ttf)$ {
try_files $uri #rootfiles;
}
# deny access to . files, for security
#
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location #rootfiles {
rewrite ^/(?:triangles|equation-of-a-line)/(.*) /$1 redirect;
}
sendfile off;
}
I basically have one site at root /usr/share/nginx/html;.
How can I change this to allow multiple sites with each site hosted at the same root directory?
Also, I have cname * mapped to the IP address.
How can I change this so that each website has its own cname?

Nginx Config for HTTPS (WWW to non-WWW and all HTTP to HTTPS)

I am unable to get my HTTP --> HTTPS rules working correctly in my NGINX config. In addition to HTTPS, I'm also trying to redirect WWW to NON-WWW. However, I can't get WWW to even work.
Here is where I'm at:
http://example.com --> https://example.com (WORKS)
http://example.com?id=3 --> https://example.com?id=3 (WORKS WITH QUERY STRING)
http://www.example.com -> https://example.com (DOESNT WORK)
https://www.example.com (DOESN'T WORK)
URL's with WWW show "Server not found. Firefox can't find the server at www.example.com."
server {
listen 80;
server_name www.example.com;
return 301 https://example.com$request_uri;
# this doesn't work. trying to http://www.example.com --> https://example.com
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
# this works. http://example.com -> https://example.com
}
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/sslmate/www.example.com.chained.crt;
ssl_certificate_key /etc/sslmate/www.example.com.key;
server_name www.example.com;
return 301 https://example.com$request_uri;
# this has www in server_name, and doesn't work
}
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/sslmate/www.example.com.chained.crt;
ssl_certificate_key /etc/sslmate/www.example.com.key;
server_name example.com;
root /home/garrett/domains/example.com/public_html;
access_log /home/garrett/domains/example.com/logs/access.log;
error_log /home/garrett/domains/example.com/logs/error.log;
index index.php index.html index.htm;
error_page 404 /404.php;
error_page 403 /404.php;
location / {
#try_files $uri $uri/ /index.php;
}
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm-garrett.sock;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
# this block works, because I can access https://example.com correctly
}
What am I doing wrong?

Resources