server {
listen 80;
listen [::]:80;
server_name _;
root /home/ec2-user;
# location / {
# proxy_pass http://sdc_servers;
# }
location /loaderio-73cc9f7580b0a0844a502ff1c98e9305.txt {
proxy_pass http://3.15.28.77/loaderio-73cc9f7580b0a0844a502ff1c98e9305/;
}
}
I am not sure where I am going wrong trying to serve this file, I am totally lost
location /loaderio-73cc9f7580b0a0844a502ff1c98e9305.txt { proxy_pass http://3.15.28.77/loaderio-73cc9f7580b0a0844a502ff1c98e9305/; }
Instead of passing the token in the proxy_pass, try something like
location /loaderio-9f8a9889007f8a2721b6245b80344b75.txt { root /<location_to_the_file>/loader/; try_files /loader.txt =404; }
loader - directory containing loader.txt which contains the actual
token provided by loader.io.
Related
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I must set root in server, not in location. If I set it this way:
server {
listen 80;
server_name www.mydomain.com;
location / {
root /opt/html;
index index_sc.html;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
}
Then I can access /imgproxy content without problem. But if I move the root out of location like below, /imgproxy will cause 404 error:
server {
listen 80;
server_name www.mydomain.com;
root /opt/html;
location / {
index index_sc.html;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
}
I want to know how exactly can I set root in server and still has location /imgproxy/ work? Thanks!
I cannot reproduce this behaviour, but you can explicitly handle the /imgproxy URI by adding another location block:
root /opt/html;
location / {
index index_sc.html;
}
location = /imgproxy {
return 301 /imgproxy/;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
Ensure that you clear the browser cache between changes to the nginx configuration.
I ham trying to make http://example.com serve http://example.com/home.html from /home/ubuntu/mysitedir/home.html.
I have the following conf file which successfully redirects everything to https, and proxies to uwsgi. The http->https redirection works fine, and the uwsgi proxy works, but http(s)://example.com/, http(s)://example.com/home.html, http(s)://example.com/index.html, and http(s)://example.com/index.htm are all 404
Any pointers as to what I can try?
Here is my conf file:
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
root /home/ubuntu/mysitedir/;
index home.html;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example_combined.crt;
ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
root /home/ubuntu/mysitedir/;
index home.html;
location /images/ads {
alias /home/ubuntu/mysitedir/images/ads/;
}
location /images {
alias /home/ubuntu/mysitedir/images/;
}
location /static {
alias /home/ubuntu/mysitedir/static/;
}
location / {
alias /home/ubuntu/mysitedir/;
include uwsgi_params;
uwsgi_pass unix:/tmp/mysitedir.sock;
}
}
Thanks.
location / is the catch-all. You could try the try_files directive like this:
location #wsgisite {
include uwsgi_params;
uwsgi_pass unix:/tmp/mysitedir.sock;
}
location / {
index home.html;
try_files $uri $uri/ #wsgisite;
}
Any thing coming into the base location will first check to see if it is a file, or a directory with an index (home.html) file in it, and if not, then pass it on to the #wsgisite.
This should also make the other three location directives obselete, since nginx will be checking for the files first before passing it to the wsgi block.
I actually have this working, but I'd like to know if I am doing it the most efficient way, or if there are any improvements I can make to my conf file. Here is what I am attempting to do:
If any file is requested from the root, we should always serve "index.html". No other file should be accessible, and requesting anything else should be treated as if you requested "index.html". Currently I'm using rewrite, but a redirect would be okay too, and possibly preferable.
Any file under "/css" or "/js" can be requested, and requesting files from those directories that don't exist should return a 404.
Here's my current working conf file:
server {
listen 80;
server_name www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
location = /index.html {
}
# map everything in base dir to one file
location ~ ^/[^/]*$ {
rewrite ^/[^/]*$ /index.html;
}
location ~ ^/css/ {
}
location ~ ^/js/ {
}
}
UPDATE
My final conf file, which is both faster under a load test and simpler than the original, is here:
server {
listen 80;
server_name example.com;
root /var/www/register;
location = /index.html {
}
# Default location, request will fallback here if none other
# location block matches
location / {
rewrite ^.*$ /index.html redirect; # 'root' location '/'
}
location /css/ {
}
location /js/ {
}
}
I'm not sure if I got this right or not, but check this answer, you always want to server index.html so it should be the default location location /
server {
server_name example.com www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
index index.html;
location / {
try_files index.html =404;
}
location /(css|js) {
try_files $uri =404;
}
}