I have been tasked with a couple project.
We have two directories on our server, one is
http://example.com/app
and the other is
http://example.com/fw
What I have been asked to do, is redirect from http to https if any visitor lands on a page in these two directories (app and fw)
Here is what I have done so far in the config file. When I added the lines to my config file below 'server' and restarted the site would not come back up. Unfortunately I don't have access to the log files. Appreciate anyone willing to take a look at this
location ~ ^/miner/memclub/.+\.html$ {
rewrite ^(.+)\.html$ /bootstrap.php?file=$1.html last;
rewrite ^(.+)\.phtml$ /bootstrap.php?file=$1.phtml last;
error_page 404 = /404.php;
}
server {
server_name site.org;
server_name *.site.org;
location /app {
if ( $scheme = http ) {
rewrite ^ https://site.org/app last;
}
}
}
First of all I don't think you can have 2 server_name, merge those two lines into one line
server_name example.com *.example.com;
And to do the https redirect i would recommend using 2 separate servers, you need one listening to port 443 anyway
server {
server_name example.com www.example.com; # which ever you are using
listen 443 ssl;
location / {
# all your https configuration
}
}
server {
server_name example.com www.example.com:
listen 80;
location /app {
return 301 https://$http_host$request_uri;
}
location /fw {
return 301 https://$http_host$request_uri;
}
location / {
# the rest of the non https configuration
}
}
I know you can merge both app and fw into one location, but I believe doing it without regex is faster, if you want to do it anyways here it is
location /(app|fw) {
return 301 https://$http_host$request_uri;
}
Related
I want to transfer all post requests to port 5000 as that's where my express server is running.
This is my nginx config at the moment:
server {
if ($host = www.website.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
server_name www.website.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.website.com;
ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; # managed$
ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem; # manag$
I know that I should have probably thought about it before writing the code and make all post requests to /api and then redirect it from the config. But I haven't, and I don't want to change the code if that's node necessary.
How can I recognize if a request is a post request and transfer it to port 5000?
Also, in what language is this config file written in? it looks like js but it isn't really
You'd better change your code, but as a quick and dirty hack you can try
server {
...
if ($request_method = POST) {
rewrite ^ /api$request_uri last;
}
location / {
# your default location config here
}
location /api/ {
proxy_pass http://127.0.0.1:5000/;
}
}
NGINX config is not a programming language, it uses its own syntax and is rather declarative than imperative, changing the order of nginx directives (except of those from ngx_http_rewrite_module) usually make no difference.
How can I make nginx redirect all the requests to my subdomain to a folder?
Example:
http://sub2.sub1.domain.com/
that should indicate that sub2 is a folder in sub1.domain.com/sub2
How can I do this?
The main objective is to hide the folder to the user. So, it should continue as
http://sub2.sub1.domain.com/
My wish is to use a wildcard in sub2.
UPDATE:
I've tried:
server {
listen 80;
listen [::]:80;
server_name ~^(.*)\.sis\..*$;
location / {
proxy_pass http://sis.mydomain.com/$1$request_uri;
}
}
but it also didn't work, any error?
In the nginx directives for sub2.sub1.domain.com you'd put:
server {
listen 80;
server_name sub2.sub1.domain.com;
location / {
proxy_pass https://sub1.domain.com/sub2;
}
}
So any request going to sub2.sub1.domain.com gets proxied to → sub1.domain.com/sub2 (while masked as sub2.sub1.domain.com); no need for a redirect or rewrite this way either.
Wildcard Method
server {
listen 80;
server_name ~^(.*)\.sub1\.domain\.com;
location / {
proxy_pass https://sub1.domain.com/$1;
}
}
*the wildcard method above is untested.
Hi guys so I have this redirect in my nginx config to ensure all pages use https, however I want to add a condition to stop the redirect if the url matches $hostname/videos/:some_id, only if videos is directly after the host as I have other urls such as $hostname/admin/videos/:some_id which still need to be https.
How can I do that?
if ($http_x_forwarded_proto != 'https') {
return 301 https://$server_name$request_uri;
}
Thanks in advance!
Well, the simplest way without using if statements is to have two different server blocks. One for HTTP and one for HTTPS.
server {
listen 80;
server_name your_host_name;
location /videos {
...
}
location / {
return 301 https://$host$request_uri;
}
}
server {
server_name your_host_name;
listen 443 ssl;
location / {
...
}
}
I have a url api.domain.com that I am trying to redirect to domain.com/api BUT I want to preserve the look that it's still api.domain.com
I have tried the following
server {
listen 80;
server_name api.domain.com;
location ~ ^/ {
rewrite ^/(.*) https://domain.com/api/$1 break;
}
}
But when I hit an endpoint eg api.domain.com/user/1 in the browser it displays as domain.com/api/user/1.
Is it possible to get the domain to visually stay as api.domain.com/user/1
I see that you want to serve api.domain.com from domain.com/api. You just need to define the root parameter accordingly.
Assuming the root directive for domain.com is /server/path/to/domain.com/html, in your case, it will become /server/path/to/domain.com/html/api.
server {
listen 80;
server_name api.domain.com;
root /server/path/to/domain.com/html/api;
location / {
# Whatever you want to do
}
}
I use Nginx as a web server and I want to deny access to a particular directory from certain domains. Or in other words, make that path/directory only accessible from one domain or IP address.
Example:
http://domain.com/manager/ => Redirects to 404 page
http://www.domain.com/manager/ => Redirects to 404 page
http://10.10.10.10/manager/ => allows access
Finally, to make this a complete solution, I'd like to force an https connection when accessing to this particular path.
Following my previous example:
http://10.10.10.10/manager/ => Rewrites to https
Using
location / {
deny all;
allow [ip];
}
And restart your services.
More : http://nginx.org/en/docs/http/ngx_http_access_module.html
To address the force https (Assuming you already have a nginx that listens to port 443)
server {
listen 80;
server_name 10.10.10.10;
rewrite ^(.*) https://10.10.10.10$1 permanent;
}
To address the redirect to 404, you can consider using the following in your main config that listens for server "domain.com"
location ~ ^/manager {
return 404;
}
This is what I was looking for:
server {
listen 80;
server_name domain.com;
# Redirect path to secure connection
location ^~ /manager {
rewrite ^/(.*) https://domain.com$1 permanent;
}
}
server {
listen 443;
location ^~ /manager {
allow 10.10.10.10;
deny all;
error_page 403 =404 /404.html;
}
}