Hy everyone,
I have a little problem with nginx server block and I hope that someone from here will know the solution to it.
This is how my configuration looks like:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com;
}
server{
listen 80;
server_name *.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name .example.com;
...
}
So the first block is normal http redirecting to https second is catching subdomains and third is where location blocks are etc.
My problem is that when the second block catches the request would like to add /admin to URL so it would be subdomain.example.com/admin but i have to check if it already has /admin so i don't get something like this subdomain.example.com/admin/admin.
I tried server_name *.example.com$ and server_name ~.example.com(=<id>.*) then if ($id = '') and hundreds of combinations and didn't get anything usefull.
Did anyone here had similar problem and solved it?
server {
listen 80;
server_name *.example.com;
if ($request_uri !~ ^/admin/) {
return 301 https://$host/admin$request_uri;
}
return 301 https://$host$request_uri;
}
Related
In my /conf.d/ folder I have a .conf file as follows:
server {
listen 80;
listen [::]:80;
return 301 https://www.example.com$request_uri;
}
You can see that it redirect any http traffic to https, that is fine.
However, there are certain http URLs, like http://www.example.com/profile/(.*) that I want to 404 immediately, without the redirect to the https version first.
I have tried variants of the following code with no success, the URL, eg http://www.example.com/profile/abc123, is always redirected to the https version.
server {
listen 80;
listen [::]:80;
location ~* ^/profile/ {
return 404;
}
return 301 https://www.example.com$request_uri;
}
Note that I want to capture example.com/profile/ and any pages that match that, eg example.com/profile/abc123 etc
Thank you.
server {
listen 80;
listen [::]:80;
location ~ ^/profile/ {
return 404;
}
server_name example.com *.example.com;
location ~ ^/ {
return 301 https://www.example.com$request_uri;
}
}
my current setup of webpage is:
forum.xyz.pl
I need xyz.pl redirect to forum.xyz.pl
current nginx.conf:
nodebb.conf
I am using aws route53, not sure what value should I put there for root domain also.
thanks
pl to forum.xyz.pl you can simply do:
server {
server_name xyz.pl;
rewrite ^ forum.xyz.pl$request_uri? permanent;
}
This should solve your problem, let me know if you have any other problems. I don't really understand the problem with Route 53 since it is just handling the DNS entries.
I'd do it like this
server {
listen 80;
server_name xyz.pl;
return 301 https://forum.xyz.pl/;
}
server {
listen 80;
server_name forum.xyz.pl;
#Force Https
return 301 https://$host$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
#listen [::]:80;
#listen 80;
server_name forum.xyz.pl;
##rest of config goes here
}
I want do a redirect from old url:
http://example.org/xxxxxxxxx.html
To new urls (remove ".html")
http://example.org/xxxxxxxxx
How I can do this with nginx?
EDIT:
xxxxxxxxx can be differ, example:
http://example.org/url-1.html redirect to http://example.org/url-1
http://example.org/another-url.html redirect to http://example.org/another-url
location ~ ^(.*)\.html$ {
return 301 $1;
}
Probably you need a rewrite statement
location /xxx.html {
rewrite ^/xxx(.*) http://example.org/xxxxx permanent;
}
You detailed explanation please refer https://www.nginx.com/blog/creating-nginx-rewrite-rules/
Another method would be return directive
server {
listen 80;
listen 443 ssl;
server_name www.old-name.com old-name.com;
return 301 $scheme://www.new-name.com;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.org www.example.org;
return 301 http://$server_name$request_uri;
}
I can't figure this out. Can you help?
This is my setup:
Single website on server.
Going to http://... worked fine until I added the https://... settings to my site config.
Going to https://... now works fine.
Going to http://... now just times out.
server {
listen 80;
server_name mywebsite.io www.mywebsite.io;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name mywebsite.io www.mywebsite.io;
root /var/www/mywebsite.io/public_html;
index index.html index.htm;
ssl on;
ssl_certificate /etc/ssl/certs/cert_chain.crt;
ssl_certificate_key /etc/ssl/certs/mywebsite.key;
}
Am I doing something stupid?
Cheers
Try:
server {
listen 80;
server_name mywebsite.io www.mywebsite.io;
return 301 https://$host$request_uri;
}
As $server_name is ambiguous when you have more than one.
I been struggling for a while to find a solution for a problem that I have:
I need to redirect all http request to https excepting a specific page under a certain location.
I have the following:
server {
listen 80;
listen 443 ssl;
server_name myserver;
root /var/www/example.org/htdocs;
index index.html;
.
.
}
I can't use:
server {
listen 80;
server_name my.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
.
.
}
I found a good solution in here
The problem with the solution is that the redirect gets into an infinitive loop, the solution is something like, after my changes:
map $uri $example_org_preferred_proto {
default "https";
~^/post/ "http";
}
server {
listen 80;
listen 443 ssl;
server_name example.org;
root /var/www/example.org/htdocs;
if ($example_org_preferred_proto = "https") {
return 301 $example_org_preferred_proto://example.org$request_uri;
}
}
I understand logic of the problem:
I get a request like: http://wwww.example.com/test -> redirects to https://wwww.example.com/test
Now I get a request like: https://wwww.example.com/test -> redirects to https://wwww.example.com/test
Now I get a request like: https://wwww.example.com/test -> redirects to https://wwww.example.com/test and got into a loop ....
I need a way to stop after redirect one time.
server {
listen 80;
server_name wwww.example.com;
root /var/www/example.org/htdocs;
location /test {
try_files $uri $uri/ =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
## here goes other ssl staff which you can find by link:
## https://gist.github.com/paskal/628882bee1948ef126dd
server_name example.org;
root /var/www/example.org/htdocs;
location /test {
try_files $uri $uri/ =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
Best solution is simplest one. You need to serve only one location and redirect anything else - so do it.
If you have problem with using two server blocks, please describe it in detail.