Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

NGINX: disallow public IP access + rewrite base URL

I am using NGINX to try and make a Django / uWSGI website publicly unreachable via bare IP address while setting up a domain with these rules:

  • my-domain.com/my-new-base should be reachable via my-domain.com,
  • my-domain.com/my-new-base should not be reachable directly (or at least should redirect to my-domain.com).

I wrote the config as follows, but I do not seem to be able to write the locations in a way that works:

server {
    listen 80 default_server;
    server_name "";

    return 444;
}

server {
    listen 80;
    server_name my-domain.com;

    location = / {
        rewrite ^/(.*)$ /my-new-base$1 break;
        include /etc/nginx/uwsgi_params;
        uwsgi_pass app:8000;
    }

    location /my-new-base {
        return 301 $scheme://my-domain.com;
    }
}

Here are the uWSGI params I am including above:

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

But all this results in an infinite loop.

question from:https://stackoverflow.com/questions/65951129/nginx-disallow-public-ip-access-rewrite-base-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...