W
Wasp-langā€¢2mo ago
cmilazzo

recommended ssl configuration for api server

@kapa.ai I am installing a prduction Wasp server with nginx as a proxy server. I've configured port 80 and 443 on nginx to forward to port 3000 on the wasp server. However, authentication fails because the client REACT_APP_API_URL is set to port 3001. I believe I should change this to https (port 443) but doing so causes the following error: "useAuth.ts:23 GET https://thehatchery.io:3001/auth/me net::ERR_SSL_PROTOCOL_ERROR" How should I be configuring the port forwarding and listening here, and what should my ENV variables be pointing to?1
22 Replies
kapa.ai
kapa.aiā€¢2mo ago
Hi @cmilazzo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. The knowledge sources provided do not contain specific information about SSL configuration for the Wasp API server or how to resolve the net::ERR_SSL_PROTOCOL_ERROR you're encountering. However, I can tell you that the REACT_APP_API_URL environment variable should be set to the URL of your backend server, and the WASP_WEB_CLIENT_URL should be set to the URL where your frontend app is running. These are used by Wasp to properly configure the connections between your frontend and backend. For example, in your .env.client file, you might have:
REACT_APP_API_URL=<https://api.yourdomain.com>
REACT_APP_API_URL=<https://api.yourdomain.com>
And in your .env.server file, you might have:
WASP_WEB_CLIENT_URL=<https://yourdomain.com>
PORT=3001
WASP_WEB_CLIENT_URL=<https://yourdomain.com>
PORT=3001
This assumes that your backend API is running at <https://api.yourdomain.com> and your frontend is accessible at <https://yourdomain.com.> The PORT variable in .env.server sets the port that your server listens for requests on. Please refer to the Wasp documentation (https://wasp-lang.dev/docs/advanced/deployment/manually#environment-variables) for more details on these environment variables. As for the SSL error, it's possible that there's an issue with your SSL certificate or configuration. You might want to check that your certificate is correctly installed and that your server is properly configured to use SSL. Unfortunately, I can't provide more specific advice without additional information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzoOPā€¢2mo ago
@kapa.ai does the WASP_SERVER_URL config item apply to the web client or the server?
kapa.ai
kapa.aiā€¢2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The WASP_SERVER_URL configuration item applies to the server. It is an environment variable that the server uses to properly redirect users when logging in with OAuth providers like Google or GitHub. In the context of OAuth setup, this is the URL of your Wasp server and it's used to generate the redirect URL for the OAuth providers. For example, in your server environment variables, you might have:
WASP_SERVER_URL=<https://your-wasp-server-url.com>
WASP_SERVER_URL=<https://your-wasp-server-url.com>
In development, Wasp sets the WASP_SERVER_URL to <http://localhost:3001> by default. If you are deploying your app, you will need to define the WASP_SERVER_URL server environment variable in your deployment environment. You can read more about this in the Wasp documentation (https://wasp-lang.dev/docs/migrate-from-0-12-to-0-13). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzoOPā€¢2mo ago
@kapa.ai Do I need to have two HTTPS ports open in my firewall? one for the wasp server and one for the api server? If I'm using nginx as a reverse proxy, how do I confure that?
kapa.ai
kapa.aiā€¢2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Based on the information provided in the knowledge sources, you would typically need two ports open for a Wasp application: one for the frontend (web client) and one for the backend (API server). However, if you're using Nginx as a reverse proxy, you can configure it to route traffic from a single external port to the appropriate internal ports. Here's a basic example of how you might set up your Nginx configuration:
server {
listen 80;
server_name yourdomain.com;

location / {
proxy_pass <http://localhost:3000;> # Forward requests to Wasp web client
}

location /api {
proxy_pass <http://localhost:3001;> # Forward API requests to Wasp server
}
}
server {
listen 80;
server_name yourdomain.com;

location / {
proxy_pass <http://localhost:3000;> # Forward requests to Wasp web client
}

location /api {
proxy_pass <http://localhost:3001;> # Forward API requests to Wasp server
}
}
In this example, all requests to <http://yourdomain.com> are forwarded to the Wasp web client running on port 3000, and all requests to <http://yourdomain.com/api> are forwarded to the Wasp server running on port 3001. For HTTPS, you would need to include additional configuration to enable SSL, specify your certificate and private key, and listen on port 443. The specifics of this configuration can vary depending on your setup and requirements. Please note that this is a basic example and might not cover all your needs. For a production setup, you would likely need additional configuration for things like logging, error pages, and so on. You should refer to the Nginx documentation (https://nginx.org/en/docs/) for more detailed information. As for the environment variables, you would typically set REACT_APP_API_URL in your .env.client file to the URL of your API server (including the /api path if you're using the above Nginx configuration), and WASP_WEB_CLIENT_URL in your .env.server file to the URL of your frontend app. For example: .env.client:
REACT_APP_API_URL=<https://yourdomain.com/api>
REACT_APP_API_URL=<https://yourdomain.com/api>
.env.server:
WASP_WEB_CLIENT_URL=<https://yourdomain.com>
WASP_WEB_CLIENT_URL=<https://yourdomain.com>
Please note that the knowledge sources provided do not contain specific information about configuring Nginx as a reverse proxy for a Wasp application, so you may need to adjust this based on your specific requirements and environment. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzoOPā€¢2mo ago
thank you @kapa.ai that seems to be almost correct. The only problem I see with it now is that I'm getting a 404 not found on all the calls into the api, as they do not seem to recognize the /api path
kapa.ai
kapa.aiā€¢2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- If you're getting a 404 error on all API calls, it's possible that the /api path isn't being correctly routed to your Wasp server. This could be due to a misconfiguration in your Nginx setup or your Wasp application. In your Nginx configuration, make sure that the /api location block is correctly set up to proxy requests to your Wasp server. Here's the relevant part of the configuration:
location /api {
proxy_pass <http://localhost:3001;> # Forward API requests to Wasp server
}
location /api {
proxy_pass <http://localhost:3001;> # Forward API requests to Wasp server
}
This configuration forwards all requests that start with /api to the Wasp server running on port 3001. In your Wasp application, make sure that your API routes are correctly defined. For example, if you have an API route defined as /users, you would access it through <https://yourdomain.com/api/users> with the above Nginx configuration. Also, ensure that the REACT_APP_API_URL environment variable in your .env.client file is set to the correct URL, including the /api path:
REACT_APP_API_URL=<https://yourdomain.com/api>
REACT_APP_API_URL=<https://yourdomain.com/api>
This tells your Wasp client where to send API requests. If you're still having issues, you might want to check your Nginx access and error logs for more information about what's going wrong. You can also try accessing your API routes directly to see if they're working as expected. Please note that the knowledge sources provided do not contain specific information about troubleshooting this issue, so you may need to adjust this based on your specific requirements and environment. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzoOPā€¢2mo ago
@kapa.ai. That didn't fix my issue. Here is my nginx configuration for the api: location /api { proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $http_host; proxy_set_header Connection 'upgrade'; proxy_bind 172.31.13.166; proxy_pass http://172.31.13.166:3001/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } and here are my environment variables: xport WASP_WEB_CLIENT_URL=https://thehatchery.io/ export WASP_SERVER_URL=http://thehatchery.io:3001/ export REACT_APP_API_URL=https://thehatchery.io/api
kapa.ai
kapa.aiā€¢2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Your Nginx configuration and environment variables seem to be set up correctly. However, the issue might be related to the trailing slash in the proxy_pass directive in your Nginx configuration. When you include the trailing slash in the proxy_pass directive like this:
proxy_pass <http://172.31.13.166:3001/;>
proxy_pass <http://172.31.13.166:3001/;>
Nginx will replace the part of the request URI that matches the location directive with the path specified in the proxy_pass directive. In your case, this means that a request to <https://yourdomain.com/api/users> would be forwarded to <http://172.31.13.166:3001/users,> not <http://172.31.13.166:3001/api/users> as you might expect. To fix this, you can remove the trailing slash from the proxy_pass directive:
proxy_pass <http://172.31.13.166:3001;>
proxy_pass <http://172.31.13.166:3001;>
This will preserve the original request URI when forwarding the request to your Wasp server. Please note that the knowledge sources provided do not contain specific information about this issue, so you may need to adjust this based on your specific requirements and environment. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzoOPā€¢2mo ago
@kapa.ai. Any other suggestions? Removing the trailing slash did not make any difference
kapa.ai
kapa.aiā€¢2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry to hear that the issue persists. Without more specific information or error messages, it's difficult to diagnose the problem. However, here are a few more things you could check: 1. Nginx Configuration: Ensure that your Nginx configuration is correct and that it's properly routing traffic to your Wasp server. You might want to check the Nginx error logs for any relevant messages. 2. Wasp Server: Make sure that your Wasp server is running and that it's listening on the correct port (3001 in your case). You can check this by trying to access your Wasp server directly at <http://172.31.13.166:3001.> 3. Firewall Rules: Check your firewall rules to ensure that traffic is allowed to your Wasp server on port 3001. 4. API Routes: Verify that your API routes are correctly defined in your Wasp application. If you have an API route defined as /users, you should be able to access it at <https://yourdomain.com/api/users> with your current setup. 5. Environment Variables: Double-check your environment variables to ensure they're correctly set. In particular, REACT_APP_API_URL should be set to <https://yourdomain.com/api.> If none of these suggestions help, you might want to consider seeking help from the Wasp community or from a network administrator who can help diagnose the issue. Please note that the knowledge sources provided do not contain specific information about troubleshooting this issue, so you may need to adjust this based on your specific requirements and environment. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzoOPā€¢2mo ago
@kapa.ai. I've verified my nginx configuration is working fine. The request gets to the Wasp server. However, it's not mapping the /api path to the auth code (/api/auth/me), and is thus not able to find the route on the server. I believe this may be a bug in wasp
kapa.ai
kapa.aiā€¢2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Based on your description, it seems like the issue might be related to how Wasp is handling the /api path. If the request is reaching the Wasp server but the server is not correctly mapping the /api path to the auth code, it could indeed be a bug or a limitation in Wasp. Wasp currently does not have built-in support for changing the base path of the API routes. All API routes in Wasp are defined relative to the root (/). For example, if you have an API route defined as /auth/me in Wasp, it expects the request to come in at /auth/me, not /api/auth/me. As a workaround, you might consider removing the /api prefix in your Nginx configuration and adjust your environment variables accordingly. However, this might not be ideal depending on your setup and requirements. If you believe this is a bug or a missing feature in Wasp, I would recommend reporting it on the Wasp GitHub issues page (https://github.com/wasp-lang/wasp/issues). The Wasp developers are very responsive and might be able to provide a fix or a better workaround. Please note that the knowledge sources provided do not contain specific information about this issue, so you may need to adjust this based on your specific requirements and environment. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
cmilazzo
cmilazzoOPā€¢2mo ago
@sodic or @miho perhaps you can help? I've been going around in circles with kapa. I'm trying to figure out how to actually deploy with SSL. I've got a reverse proxy setup to forward port 80 and 443 to 3000 for the web server itself just fine. BUT, the api calls also have to be SSL or the browser causes them to fail (especially the authentication calls!), so I tried to add a listener in my proxy on port 443 to redirect /api to port 3001 on the server.... apparently that's not supported. Any thoughts on how this should actually be done? Many thanks! Essentially, I am now getting 404 errors on https:thehatchery.io/api/auth/me further testing.... I opened another port on my reverse proxy (488, also SSL) and forwarded that to port 3001 and changed the env as such: export WASP_SERVER_URL=http://thehatchery.io:3001 export REACT_APP_API_URL=https://thehatchery.io:488
MEE6
MEE6ā€¢2mo ago
Wohooo @cmilazzo, you just became a Waspeteer level 5!
cmilazzo
cmilazzoOPā€¢2mo ago
now I get a connection timed out network error, so that didn't help either...
miho
mihoā€¢2mo ago
Kapa is great for simpler stuff, but it can lead you down some wrong paths for more complex topics. What I'm reading is that you need some help with setting up your client app and server app with HTTPS. For this, you need to understand how ports vs. domains work. Locally, you have different apps on localhost and they need to use different ports so you can access them e.g. localhost:3000 or localhost:3001. When you deploy stuff, you generally don't want users to visit your website via IP:3000 and IP:3001 becauase it doesn't look professional. That's why we have domains e.g. https://thehatchery.io. Nginx accepts requests that come to domains and routes them internally to the app ports. The thing to note with Nginx is that one domain name -> one port. So if you have https://thehatchery.io it can go to the client app at port 3000. https://thehatchery.io:3001 won't work because https://thehatchery.io is already set up to go to localhost:3000 and the extra port just messes up how Nginx routes your request. šŸ‘‰ So, after this lengthy intro, what's the actual solution? We need a different domain or subdomain for our app at the port 3001. Subdomains are not a problem to set up if you already have a domain. For example, let's say we'll set up Nginx to route https://api.thehatchery.io -> localhost:3001 This will work becuase - it's one domain per one port and Nginx knows how to route it. For a subdomain, DNS setup is the same as for the main domain e.g. if you want the https://api.thehatchery.io subdomain, add a new A record that points to your server's IP and have a value of api.
miho
mihoā€¢2mo ago
šŸ‘‰ Here's some materials on deploying Wasp to a VPS, maybe not 100% applicable since it doesn't use Nginx, but maybe for some extra understanding šŸ™‚ https://gist.github.com/infomiho/80f3f50346566e39db56c5e57fefa1fe
Gist
Deploy Wasp to a VPS (reverse proxy + Docker)
Deploy Wasp to a VPS (reverse proxy + Docker). GitHub Gist: instantly share code, notes, and snippets.
cmilazzo
cmilazzoOPā€¢2mo ago
So, I did end up solving this last night. I've got NGINX listening on port 80/443 for the server (redirecting to port 3000 locally), and port 448 for the API server, and it forwards the 448 to port 3001 internally. Then I set REACT_APP_API_URL=https://mydomain.com:448. and WASP_SERVER_URL=http://mydomain.com:3001/ no need for a subdomain, although I suppose that might look cleaner instead of using port 448 for https
miho
mihoā€¢2mo ago
Yep, as I said, that's usually the recommendation to use multiple domains / subdomains to get a cleaner look. But what ever works for you - I'm glad you got it working! šŸ™‚
cmilazzo
cmilazzoOPā€¢2mo ago
Well, mostly. :). I'm still running into the occasional CORS issue, but I'll figure that out.
Filip
Filipā€¢2mo ago
Hey, any luck figuring it out? You probably need to set some env vars: https://wasp-lang.dev/docs/advanced/deployment/manually#2-deploying-the-api-server-backend Reply here if you get stuck!
Deploying Manually | Wasp
This document explains how to build and prepare your Wasp app for deployment.
Want results from more Discord servers?
Add your server