How to serve a single page application in a Swift iOS app? - ios

I have a Swift project with a webview that is serving files from a local folder vai GCDwebserver, however the website is designed as a single page application, is there any way I can rewrite all url requests to index.html?
I'm currently using GCDwebserver but I'm willing to change servers to get the SPA to load in the webview.
With nginx I'd do somthing like
server {
listen 8080;
listen [::] 8080;
root /root/bundled;
location / {
try_files $uri $uri/ /index.html;
}
}
How do I get the same rewrite effect on a webview in osx?

Simply use a default handler to catch all GET request:
- (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block;
Or a regex one if you want to do more fancy matching:
- (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block
Or even go all the way to a match block:
- (void)addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock;

I must admit I've never used GCDWebserver, but if you have any requirement in an iOS app to redirect a url you can use NSURLProtocol. You can read more about it here https://www.raywenderlich.com/76735/using-nsurlprotocol-swift. Once you've read the link you basically need to use the method canonicalRequestForRequest to capture the current request and return a new one. Hope that helps.

Related

Nginx redirect for a subdomain

does anyone know how the interaction works in Nginx?
I currently have a subdomain, let's call it subdomain1, I want to change it to subdomain2.
To be more specific.
I run everything in a docker container and my certificate will be for subdomain2. And there will be no more servers with subdomain1.
I want to keep the traffic from google for subdomain1, but the name is not appropriate anymore and it needs to be changed to subdomain2.
Does something like this work? Will there be any issues?
server {
server_name subdomain1.mydomain.com;
return 301 http://www.subdomain2.mydomain.com/$request_uri;
}
Something like that could match :
server {
listen 8066;
server_name localhost;
location / {
rewrite (.*)$ http://www.google.com$1 redirect;
}
}
8066 is for my test purpose to redirect to google.com.
If y try localhost:8066/foo, I go to https://www.google.com/foo
Note that redirect keyword makes it temporary. For a permanent redirection, use permanent instead.
Yes, your approach will work. Following points might be helpful:
Since you want not to have any server for subdomain1 but in this redirection you need to ensure that subdomain1 also pointing to the same server where you have hosted subdomain2
use of $scheme
server { server_name subdomain1.mydomain.com; return 301 $scheme://subdomain2.mydomain.com$request_uri; }
Generally people avoid using www before sub-domain.domain.com (you may refer this also)
Section server in nginx has two required parameters listen and server_name. Add listen to your config and it will work
Man about server https://nginx.org/en/docs/http/ngx_http_core_module.html#server
Example
server {
listen 8080;
server_name _;
return 301 http://www.google.com/$request_uri;
}

Is there a way to change my URL to point to a custom port?

As stated in the title, i would like to change (internaly) my URL from:
https://subdomain.example.tech:8081/
to something like:
https://subdomain.example.tech/something/
Is it actually possible? (it's a Docker)
thanks for your answers
You have to use a web server/load balancer which handles the requests.
So, you should handle it on its configurations, Not in Docker.
For example, if you use nginx so:
server {
listen 8081;
server_name subdomain.example.tech;
return 301 https://subdomain.example.tech/$request_uri;
}

Multiple Docker Containers Hosted In Nginx With URL Instead Of Subdomain

I'm a noob to docker, Nginx, and devops, so go easy on me.
I've followed a few tutorials that show me how to host multiple web apps through docker containers using Nginx and subdomains. I cannot create a new A Record for this domain, so I can't use subdomains, it has to be a url. If I could create a new A Record, I found a million tutorials that show me how to host it on ProjectA.example.com but since I don't have access to create a new A Record for the domain, I need to find a way to host it on something like example.com/ProjectA. Another obstacle is only port 80 is open to the outside, so all traffic must come through port 80 and be reverse proxied to whatever port the docker container is forwarding from.
So far I have an Nginx configuration that looks something like this
server {
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
listen 80;
server\_name \_;
location / {
try\_files $uri $uri/ =404;
}
location /projectA {
proxy\_pass http://127.0.0.1:9001/;
}
location /projectB {
proxy\_pass http://127.0.0.1:9002/;
}
}
This works getting me to the homepage of the project. But the CSS of the website doesn't load, and whenever I click a link, it sends me to something like example.com/signup instead of example.com/projectA/signup. I tried making a wildcard location (location \~ /projectA.\*) but Nginx didn't like that. I was thinking there's probably a way I could get something like if the referring uri contains projectA, send them to example.com/projectA$uri but I couldn't find the documentation on the syntax.
Basically the question is, is this a good way to tackle the problem, and does anyone have a link to a tutorial or some documentation on how to do this?
Using a trailing slash behind location should do it:
location /projectA/ {
proxy_pass http://127.0.0.1:9001/;
leads /projectA/whatever to http://127.0.0.1:9001/whatever
If you want to use regex to rewrite, it's something like that:
location ~ ^/projectA/(.*)$ {
proxy_pass http://127.0.0.1:9001/$1;
or
location /projectA/ {
rewrite ^/projectA/whatever/(.*)$ /whatever.php?path=$1 break;
proxy_pass http://127.0.0.1:9001/;
}
leads /projectA/whatever/foo to http://127.0.0.1:9001/whatever.php?path=foo

Serve Rails API and Ionic mobile website together

Basing on How to run Ionic serve permanently? and Deploy Ionic as a website, nginx should be able to serve the code from the Ionic's www folder. I am exploiting the idea of serving it with its Rails back-end together using the same domain address... so that no CORS traffic and overhead will be added. Another requirement for the Rails WEB is to still handle the desktop (HTML) version of the website. Essentially, there will be 3 types of requests coming to the nginx server:
loading html, js, css files from the mobile/www/ directory
mobile website and APP JSON calls to Rails API
desktop website HTML calls to Rails
Type 2 requests may be simple because they all have the .json extension. With sub-domains are taken by the username, i.e. username.example.com, any ideas on how to have nginx route the html, js, and css requests correctly? Or is this too much of a challenge?
Take #1: Come up with a Nginx config that returns Ionic files when Rails signals it in a hidden manner. May be clumsy, so please feel free to offer criticism, pitfalls, or improvements.
Nginx config:
server {
# Development logging
access_log /home/builder/projects/web/log/access.log;
error_log /home/builder/projects/web/log/error.log notice;
listen 80;
server_name projects.host www.projects.host;
# Eusure Rails' index route gets uri "/" first.
index index.html;
# All API json calls and requests to Rails controllers.
location ~ ^\/(.+\.json$|others.*|users.*|index\.html$) {
# Rails server
proxy_pass http://127.0.0.1:3000;
# The Rails server may request Ionic mobile website with a temporary redirect (status 307)
proxy_intercept_errors on;
error_page 307 = #temp_redirect;
}
# If a temporary redirect is to /mobile_web, response with Ionic mobile root.
location #temp_redirect {
if ($upstream_http_location ~ ^http.+\/\/.+\/mobile_web$) {
set $mobile true;
root /home/builder/projects/mobile/www;
}
# Something else, return it.
if ($mobile != true) {
return 307 $upstream_http_location;
}
}
# Ionic mobile root
location / {
root /home/builder/projects/mobile/www;
}
}
In RoR:
# Decide whether to handle the root action within Rails app or to
# signal the downstream server (nginx) to return Ionic mobile web.
def index
# TODO: Needs a bit of logic before the following redirect.
redirect_to '/mobile_web', status: :temporary_redirect # 307
end
Two birds with one APP :).

application stopped loading static assets after the use of nginx

I am trying to set up multiple proxies using nginx to various rails apps on one domain. These apps can be found in the domain as such: example.com/app1, example.com/app2.
After implementing this, example.com/app1 stopped loading any static assets (ex. css images). Would anyone have an idea why this is so, and how to fix this?
The URLs generated by Rails reference the root URL: /javascripts/filse.js, which is clearly incorrect. It should instead be /app1/path/to.asset
Here's a portion of my server configuration:
server {
listen 80;
location /app1/ {
root /www/app1/public;
proxy_pass http://127.0.0.1:3000/;
include proxy.conf;
}
}

Resources