gcloud appengine go1.11: cannot serve static files - google-app-engine

I have a go 1.11 app with following directory:
./language
./models
./system
./public
./public/templates
./public/templates/home
./public/templates/layouts
./public/templates/partials
./public/templates/partials/frontend
./public/assets
./public/assets/css
./public/assets/js
./public/assets/img
./public/assets/icon
./public/assets/fonts
./conf
./handlers
And my app.yaml:
runtime: go111
handlers:
- url: /favicon.ico
static_files: favicon.ico
upload: favicon.ico
secure: always
- url: /((?:img|css|js|icon|fonts)/(.*))$
static_files: public/assets/\1
upload: public/assets/
http_headers:
X-Foo: bar
secure: always
- url: ./*
script: auto
secure: always
Everything works locally with dev_appserver.py, but I get 404s for all my static assets once I deploy to AppEngine. Ideas?

Try to add:
- url: /static
static_dir: public

Related

How to configure Google App Engine app.yaml file to force https

How do I force all domain.tld and www.domain.tld to https://domain.tld? I was able to get close. If I uncomment by commented lines it will force everything but then everything in my static folder can't be accessed.
runtime: php74
env: standard
entrypoint: serve public/index.php
handlers:
#- url: /.*
# script: auto
# secure: always
# redirect_http_response_code: 301
- url: /static
static_dir: static
You need this:
runtime: php74
env: standard
entrypoint: serve public/index.php
handlers:
- url: /static
static_dir: static
- url: /.*
script: auto
secure: always
redirect_http_response_code: 301
The order is important because the first matching rule is used.

Configure Google App Engine yaml file to handle 404 Error

After the deployment of my application on Google App Engine every things works like a charm , I can access all pages but when I refresh I receive a 404 error
Example : when refreching https://my-app...appspot.com/create-ad throw 404 not found
I tried
Angular 6 routes not found on Google App Engine
and
How to configure Google App Engine yaml file to handle 404 Error
but same result
This on my app.yml
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: dist/index.html
upload: dist/index.html
- url: /
static_dir: dist
- url: /.*
static_files: dist/index.html
upload: dist/index.html
skip_files:
- ^.*node_modules(/.*)?
- ^.*json_data(/.*)?
- ^.*e2e(/.*)?
and also tried this app.yml config to redirect all url to index.html
runtime: python27
api_version: 1
threadsafe: false
service: frontend-accept
handlers:
- url: /
static_files: dist/index.html
upload: dist/index.html
- url: /
static_dir: dist
- url: /.*
script: main.py
skip_files:
- ^.*node_modules(/.*)?
- ^.*json_data(/.*)?
- ^.*e2e(/.*)?
this is my main.py
import webapp2
app = webapp2.WSGIApplication()
class RedirectToHome(webapp2.RequestHandler):
def get(self, path):
self.redirect('/dist/index.html')
routes = [
RedirectRoute('/<path:.*>', RedirectToHome),
]
for r in routes:
app.router.add(r)
But always get 404 when refreshing the page
Any help? Thanks
The reason of the 404 HTTP error code is due to this handlers:
- url:
static_dir: dist
As stated in the Google App Engine official documentation, using static_dir: dist causes that all URLs beginning with the / pattern are treated as paths to static files in the /dist directory, so for example every time you call the URL /whatever the application will look for the file /dist/whatever, since it doesn’t exist you get the 404 error.
I believe that the following code will work as you are expecting:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: dist/index.html
upload: dist/index.html
- url: /dist/index.html
static_files: dist/index.html
upload: dist/index.html
- url: /.*
script: main.app
skip_files:
- ^.*node_modules(/.*)?
- ^.*json_data(/.*)?
- ^.*e2e(/.*)?
The handler for the / endpoint will serve the dist/index.html file, as well as the /dist/index.html endpoint.
Handlers are checked sequentially and if none of the above handlers have been called then any URL matching the pattern /.* ( which is all of them) will call the main.app script (this will basically override the 404 error message).
This script is redirecting you to the /dist/index.html endpoint, so this is the reason why it needs to be handled in the yaml file.
As a final point I had to import webapp2_extras.routes to use RedirectRoute in the main.py.
finally the solution was to configure the app.yml properly
runtime: python27
api_version: 1
threadsafe: true
skip_files:
- ^(?!dist) # Skip any files not in the dist folder
handlers:
- url: /((?:runtime|main|polyfills|styles|vendor)\.[a-z0-9]+\.js)
secure: always
redirect_http_response_code: 301
static_files: dist/browser/\1
upload: dist/browser/.*
- url: /((?:runtime|main|polyfills|styles|vendor)\.[a-z0-9]+\.js\.map)
secure: always
redirect_http_response_code: 301
static_files: dist/browser/\1
upload: dist/browser/.*
- url: /(styles\.[a-z0-9]+\.css)
secure: always
redirect_http_response_code: 301
static_files: dist/browser/\1
upload: dist/browser/.*
- url: /((?:assets|docs)/.*|favicon\.ico)
secure: always
redirect_http_response_code: 301
static_files: dist/browser/\1
upload: dist/browser/.*
- url: /(manifest\.json|ngsw\.json|ngsw-worker\.js|safety-worker\.js|worker-basic\.min\.js|ngsw_worker\.es6\.js\.map)
secure: always
redirect_http_response_code: 301
static_files: dist/browser/\1
upload: dist/browser/.*
- url: /(.*\.woff)
mime_type: application/x-font-woff
secure: always
redirect_http_response_code: 301
static_files: dist/browser/\1
upload: dist/browser/.*
- url: /.*
secure: always
redirect_http_response_code: 301
static_files: dist/browser/index.html
upload: dist/browser/index\.html
http_headers:
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Frame-Options: DENY

Hosting jekyll to Google App engine

Built a personal blog using jekyll. Everything works well on local host. I do not want to deploy it on github. I prefer hosting on google app engine for some reasons.
I followed some instructions online and copied _site folder generated to my google app engine project.
this is how app.yaml looks like:
application: myblog
version: 1
runtime: python27
api_version: 1
threadsafe: yes
error_handlers:
- file: /404.html
handlers:
- url: /
static_files: _site/index.html
upload: _site/index.html
- url: /(.*)
static_files: _site/\1
upload: _site/(.*)
libraries:
- name: webapp2
version: "2.5.2"
when i run it locally on google app engine, only the index.html and some other files displays. others shows page not found. Is there anything I am not implementing properly ?
Well, I finally figure it out. It is a little trick anyway.
First in your _config.yaml file add:
permalink: /posts/:title/index.html
after that, run jekyll serve to generate the static file in _site folder. Copy the the post folder to _site/ in your app engine project.
Then, in your _config.yaml file change permalink to:
permalink: /posts/:title
run jekyll serve to generate the static file in _site. Copy the entire files generated excluding posts folder into _site/ into your app engine project.
then, make your appengine app.yaml look somewhat like this:
application: myblog
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /(.*\.js)
mime_type: text/javascript
static_files: _site/\1
upload: _site/(.*\.js)
- url: /(.*\.(jpg|png|ico))
static_files: _site/\1
upload: _site/(.*\.img)
- url: /(.*\.css)
mime_type: text/css
static_files: _site/\1
upload: _site/(.*\.css)
- url: /(.*\.(eot|svg|svgz|otf|ttf|woff|woff2))
static_files: _site/\1
upload: _site/(.*\.fonts)
- url: /
static_files: _site/index.html
upload: _site/index.html
- url: /(.+)/
static_files: _site/\1/index.html
upload: _site/(.+)/index.html
expiration: "15m"
- url: /(.+)
static_files: _site/\1/index.html
upload: _site/(.+)/index.html
expiration: "15m"
- url: /(.*)
static_files: _site/\1
upload: _site/(.*)
#- url: /((tags)|(archive)|(about)|(posts)|(fonts))/
libraries:
- name: webapp2
version: "2.5.2"
see example for clarification

Static file referenced by handler not found: index.html

After deploying my first app engine app, I'm receiving Static file referenced by handler not found: index.html as an error in the logs. Here is my yaml file:
application: section-14
version: v1
api_version: 1
runtime: python27
threadsafe: yes
handlers:
- url: /bower_components
static_dir: bower_components
- url: /general
static_dir: general
- url: /projects
static_dir: projects
- url: /js
static_dir: js
- url: /styles
static_dir: styles
- url: /elements
static_dir: elements
- url: /images
static_dir: images
#here's the problem
- url: /
static_files: index.html
upload: /
#------------------
- url: /elements.html
static_files: elements.html
upload: /
I can travel to any of the other directories, and files located in those directories, without any problem. Also, if you look below the index entry, the elements.html route works also.
I noticed in other projects that people are defining a /static directory. Is that a requirement? My local environment serves this app without any issues as is.
Naming the directory static isn't a requirement. I do it because it makes the significance of the layout obvious (to me, at least).
Here's an app.yaml fragment from one of my apps that has a static home page and static assets.
handlers:
- url: /style/
static_dir: static/style
- url: /js/
static_dir: static/js
- url: /favicon.ico
static_files: static/favicon.ico
upload: static/favicon.ico
mime_type: image/x-icon
- url: /.+
script: main.app
- url: /
static_files: static/index.html
upload: static/index.html
The order is significant, as is the use of /.+ instead of /.* With the latter, requests for / would get routed to the main.app
Edited to add: Having a url mapping for a static favicon.ico is useful to prevent the request getting routed to your app, since this'll cause App Engine to spin up an instance when one isn't needed.

Debugging 404 on GAE production server

I am receiving a 404 error for every file of my GAE project on the live server and I'm unsure of the next debugging steps
My directory structure is as follows:
| /project
|____ /build
|________ index.php
|________ other_project_files.php
Here are the relevant configuration handlers in app.yaml:
application: alex-young
version: 2
runtime: php
api_version: 1
threadsafe: true
default_expiration: "7d 0h"
skip_files:
- (.*/)?.*/node_modules/.+
- (.*/)?.*/src/.+
- (.*/)?.*/gruntfile\.js
- (.*/)?.*/todo\.txt
handlers:
# - url: /robots.txt
# static_files: robots.txt
# upload: robots.txt
- url: /Alexander\ Young\ -\ Resume.pdf
static_files: build/Alexander\ Young\ -\ Resume.pdf
upload: build/Alexander\ Young\ -\ Resume.pdf
- url: /css
static_dir: build/css
- url: /img
static_dir: build/img
- url: /js
static_dir: build/js
- url: /partials
static_dir: build/partials
application_readable: true
- url: /templates
static_dir: build/templates
application_readable: true
- url: /utils
static_dir: build/utils
application_readable: true
- url: /mustache
static_dir: mustache
application_readable: true
- url: /(.*\.(gif|png|jpg|ico|js|css|eot|svg|ttf|woff))
static_files: \1
upload: (.*\.(gif|png|jpg|ico|js|css|eot|svg|ttf|woff))
- url: /(.+\.php)$
script: build/\1
- url: /
script: build/index.php
The logs simply show a 404 error and no other information. My entire project loads fine on my local machine's MAMP and GAE test servers. I have no idea where to look for errors. Does GAE provide any more details other than Logs?

Resources