I have the Nginx config as below, I'm trying to redirect
https://example.com
to
https://www.example.com
But when I enter
https://example.com
in the browser, I don't see the redirect. Anything idea?
server {
listen 443;
server_name example.com;
ssl on;
ssl_certificate /etc/nginx/conf.d/mynginx.crt;
ssl_certificate_key /etc/nginx/conf.d/mynginx.key;
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name www.example.com;
ssl on;
ssl_certificate /etc/nginx/conf.d/mynginx.crt;
ssl_certificate_key /etc/nginx/conf.d/mynginx.key;
location /media {
alias /media; # your Django project media files - amend as required
}
location /static {
alias /static; # your Django project static files - amend as required
}
location / {
proxy_pass http://web/;
}
}
Related
I have few HTTP links to the js/css files. I wanted to redirect it to other location. Example provided below:
Redirection from:
https://ex.example.com/js/file1.js
https://ex.example.com/css/file2.css
https://ex.example.com/css/folder1/file3.css
Redirection to:
https://ex2.example.com/js/file1.js
https://ex2.example.com/css/file2.css
https://ex2.example.com/css/folder1/file3.css
I have tried below settings in nginx.conf. But its not working.
server {
listen 443 ssl;
server_name ex.example.com;
location / {
return 301 https://ex2.example.com;
}
location ~* \.(css)$ {
root https://ex2.example.com/css;
}
location ~* \.(js)$ {
root https://ex2.example.com/js;
}
#location /css/ {
# return 301 https://ex2.example.com/css;
#}
#location /js/ {
# return 301 https://ex2.example.com/js;
#}
ssl_certificate /etc/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/ssl/myserver.key;
client_max_body_size 5M;
ssl_prefer_server_ciphers on;
}
}
Please help me with this.
Use the following configuration:
server {
listen 443 ssl;
server_name ex.example.com;
location / {
return 301 https://ex2.example.com/$request_uri;
}
ssl_certificate /etc/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/ssl/myserver.key;
client_max_body_size 5M;
ssl_prefer_server_ciphers on;
}
}
/$request_uri will automatically takeup the previous request uri and will add it to the redirection uri. You don't need to match regex and do seperate redirections.
I have the following server block in Nginx config:
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
server_name _;
if ($scheme = http) {
return 302 https://$host$request_uri;
}
location / {
root /frontend/master;
index index.html;
try_files $uri $uri/ /index.html?$query_string;
}
}
So I already have redirect from http to https. But how to do redirect from www.example.com to example.com for this rule?
UPD: the most important thing here is that domains in our service are dynamic, our users register them by themselves and then chaining them via DNS settings. So I need to make a redirect to non-www without putting some domains in the server_name variable.
Try this
server_name ~^(www\.)?(?<hostname>.+)\.(?<zone>.+)$;
return 301 https://$hostname.$zone$request_uri;
You can change regexp with your hostname for example
I am struggling to implement an automatic nginx redirect from non index pages to my index page, with the exception of /admin
For instance, example.com/test should redirect to example.com, but example.com/admin should not redirect to example.com
This is my current nginx configuration file:
upstream app_server {
server unix:/tmp/mysite.sock;
}
proxy_cache_path /var/www/example.com/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name www.example.com example.com;
# redirects both www and non-www to https
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/example.com/media;
}
location /static {
alias /var/www/example.com/static;
}
location / {
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
proxy_ssl_server_name on;
}
}
I have tried adding a try_files statetement within my location / block, and other things, but none seem to work. Am I missing something?
You are trying to mix proxy_pass with try_files, it won't work within the same location block. You can use named location instead and rewrite any URI that doesn't start with /admin to a root one using negative regex assertion:
location / {
try_files $uri #app;
}
location #app {
rewrite ^(?!/admin) / break;
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
}
You don't need the separate location /media { ... } or location /static { ... } blocks, because as nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
Instead you just need to define the common server root (outside of any location blocks):
root /var/www/example.com;
You are also don't need to use the proxy_ssl_server_name directive since you are not proxying your request to the upstream with HTTPS protocol.
As a test, I enabled the nginx status page as per these articles
server {
listen 80;
#listen on any host name
server_name _;
location /status {
stub_status on;
access_log off;
}
access_log /var/log/nginx/$host-access.log;
error_log /var/log/nginx/monitor-error.log;
}
I'm normally running a wordpress site, and redirecting any http request to an https request:
server {
server_name _;
listen 80;
return 301 https://$host$request_uri;
}
I have several https server blocks, one for each dns which has it's own server cert.
Is there some way of combining the two server blocks above, so that normally an http request will redirect to https, but if the /status url is used, it will activate the nginx status page?
You need do something like below
server {
server_name _;
listen 80;
location = /status {
stub_status on;
access_log off;
}
location / {
return 301 https://$host$request_uri;
}
access_log /var/log/nginx/$host-access.log;
error_log /var/log/nginx/monitor-error.log;
}
So in case of /status no redirection will happen. In rest cases it will just do the https redirect
I have a wordpress website with https protocol by configuring the nginx 301 redirect:
server {
listen 80;
server_name xxx.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}
And my article has some image links with static server like:
http://yyy.com/1.png
But when i access this article: it will be https://yyy.com/1.png, How do I configure the nginx that can still use http for the image static server?
You would do that using below config
server {
listen 80;
server_name xxx.com;
location ~* \.(png|ico|jpeg)$ {
root <your root folder>;
try_files $uri =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}