Noobie question here.

I've setted up Parse-Server in my Ubuntu droplet and i'm currently dealing with an issue here. My ssl is from letsencrypt

In this file

/etc/nginx/sites-enabled/default

I have the following

# HTTP - redirect all requests to HTTPS
server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com;
    return 301 https://$host$request_uri;
}

# HTTPS - serve HTML from /usr/share/nginx/html, proxy requests to /parse/
# through to Parse Server
server {
        listen 443;
        server_name example.com;

        root /usr/share/nginx/html;
        index index.html index.htm;

        ssl on;
        # Use certificate and key provided by Let's Encrypt:
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

        # Pass requests for /parse/ to Parse Server instance at localhost:1337
        location /parse/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:1337/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        }
        location /test/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:1337/test/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        }
        location /dashboard/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:4040/dashboard/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        }

        location / {
                try_files $uri $uri/ =404;
        }
}

So the followin links are working fine

but they are working cause of the default file has the code to work fine. I can't do that for all the directories that parse has for example

is getting a 404 error.

Is there any way to make all the pages available without having to configure them in the default file?

Update

So when I put this inside my default file

    location /parse/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://localhost:1337/parse/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_redirect off;
    }

I get no 404 error. This is because of the proxy_pass http://localhost:1337/parse/ that is different from the first file that I've posted here.

But how can i do that for ALL the requests no matter /parse/ or /something/ etc? I cannot right down here ALL the folders and possible links that i will create inside server. Because at this domain I will also setup a website which will have /assets/ etc and it will need each single one. Isn't there any code to include ALL the possible links that it will be created?

  • You've configured that location under the "location /parse/" rule. 404 means page not found. Go look at the parse server logs, that's where you'll likely find the cause of the error. – Tim Aug 27 '16 at 19:01
  • @Tim i've updated my question. Please take a look at it :D – Kwnstantinos Natsios Aug 28 '16 at 7:27
  • When you define a rule it applies to all subdirectories. I think you need to read up on Nginx or make your question a lot more obvious. I bet if you look in your Nginx logs you'll find Nginx is passing the request on to the server on port 1337 and it's returning the 404. Post Nginx and Parse error and access logs. – Tim Aug 28 '16 at 8:03
  • @Tim yes i understand that, but do i have to do this for ALL the folders? Because in HTML I also get a 404 on assets – Kwnstantinos Natsios Aug 28 '16 at 8:14
  • You haven't described your requirements, architecture, or problem well enough for me to help any further. – Tim Aug 28 '16 at 19:40
up vote 1 down vote accepted

If you want most requests go to the proxy, but only serve some content directly from nginx, you can use this structure:

location /path/to/nginx/content {
    try_files $uri $uri/ =404;
}

location /dashboard {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:4040/dashboard/;
    proxy_set_header Host $http_host;
    proxy_redirect off;
}

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:1337;
    proxy_set_header Host $http_host;
    proxy_redirect off;
}

So, first we specify the exceptions to the default procedure, and then the default case.

Note that there is no ending / in the proxy_pass directive in the default case. It means that nginx will append the URI to the request sent to the proxy server, that is, https://example.com/some/path will be proxied to http://localhost:1337/some/path.

I removed the proxy_ssl_session_reuse directive, since it means nothing when the connections to proxy are made with http.

If the proxy connections are made with https, even then it is not useful to disable session resuse, since that degrades performance due to new SSL session negotiation on every request to the proxy.

Then you need to make sure that the root directive specifies a correct directory where nginx can find the static resources. For the root option in your configuration, https://example.com/path/to/nginx/content/example.gif needs to be at /usr/share/nginx/html/path/to/nginx/content/example.gif on the filesystem.

  • Thanks a lot for the answer and for clearing up a bit the situation. Want to ask you one question. In the last part of your example where you write location / { why localhost has the port 1337? Shouldn't it be 8080? I dont know if i'm right or wrong. I'm just asking! – Kwnstantinos Natsios Aug 28 '16 at 14:02
  • You had used port 1337 in your configuration, so I assume that this is the port where you have your application server running. In case your application server uses some other port, go ahead and change it. – Tero Kilkanen Aug 28 '16 at 16:36
  • Yeap that is correct :D – Kwnstantinos Natsios Aug 29 '16 at 8:21

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.