Skip to content

Commit 50b6485

Browse files
committed
prod: serve css, js and images from separate domain.
Fix #73
1 parent 4b94639 commit 50b6485

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
server {
2+
listen 80;
3+
server_name stamps.filezz.ru;
4+
server_tokens off;
5+
error_log /data/logs/nginx-static.log info;
6+
7+
location / {
8+
return 301 http://my-stamps.ru;
9+
}
10+
11+
location /image/ {
12+
proxy_set_header X-Forwarded-For $remote_addr;
13+
proxy_pass http://127.0.0.1:8080;
14+
}
15+
16+
location ~* \.(ico|css|js)$ {
17+
proxy_set_header X-Forwarded-For $remote_addr;
18+
proxy_pass http://127.0.0.1:8080;
19+
}
20+
21+
}

src/main/config/nginx/mystamps.conf

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ map $http_accept_language $lang {
44
}
55

66
server {
7-
listen *:80;
7+
listen 80 default_server;
8+
server_name my-stamps.ru www.my-stamps.ru;
89
server_tokens off;
910
error_log /data/logs/nginx.log info;
1011

@@ -14,6 +15,14 @@ server {
1415
try_files /503.$lang.html =502;
1516
}
1617

18+
location /image/ {
19+
return 301 http://stamps.filezz.ru$request_uri;
20+
}
21+
22+
location ~* \.(ico|css|js)$ {
23+
return 301 http://stamps.filezz.ru$request_uri;
24+
}
25+
1726
location / {
1827
error_page 502 =503 @maintenance;
1928
proxy_set_header X-Forwarded-For $remote_addr;

src/main/java/ru/mystamps/web/Url.java

+18-10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
public final class Url {
3131
public static final String PUBLIC_URL = "http://my-stamps.ru";
32+
public static final String STATIC_RESOURCES_URL = "http://stamps.filezz.ru";
3233

3334
// defined at pom.xml (and used by functional tests only)
3435
public static final String SITE = "http://127.0.0.1:8080";
@@ -85,7 +86,7 @@ public final class Url {
8586
private Url() {
8687
}
8788

88-
public static Map<String, String> asMap(boolean useCdn) {
89+
public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
8990
// There is not all urls but only those which used on views
9091
Map<String, String> map = new HashMap<>();
9192
map.put("PUBLIC_URL", PUBLIC_URL);
@@ -101,19 +102,26 @@ public static Map<String, String> asMap(boolean useCdn) {
101102
map.put("ADD_COUNTRY_PAGE", ADD_COUNTRY_PAGE);
102103
map.put("INFO_COUNTRY_PAGE", INFO_COUNTRY_PAGE);
103104
map.put("INFO_COLLECTION_PAGE", INFO_COLLECTION_PAGE);
104-
map.put("GET_IMAGE_PAGE", GET_IMAGE_PAGE);
105-
map.put("FAVICON_ICO", FAVICON_ICO);
106-
map.put("MAIN_CSS", MAIN_CSS);
107-
map.put("CATALOG_UTILS_JS", CATALOG_UTILS_JS);
108105

109-
if (useCdn) {
110-
map.put("BOOTSTRAP_CSS", BOOTSTRAP_CSS_CDN);
111-
map.put("BOOTSTRAP_JS", BOOTSTRAP_JS_CDN);
112-
map.put("JQUERY_JS", JQUERY_JS_CDN);
113-
} else {
106+
if (serveContentFromSingleHost) {
114107
map.put("BOOTSTRAP_CSS", BOOTSTRAP_CSS);
115108
map.put("BOOTSTRAP_JS", BOOTSTRAP_JS);
116109
map.put("JQUERY_JS", JQUERY_JS);
110+
map.put("GET_IMAGE_PAGE", GET_IMAGE_PAGE);
111+
map.put("FAVICON_ICO", FAVICON_ICO);
112+
map.put("MAIN_CSS", MAIN_CSS);
113+
map.put("CATALOG_UTILS_JS", CATALOG_UTILS_JS);
114+
} else {
115+
// Use separate domain for our own resources
116+
map.put("GET_IMAGE_PAGE", STATIC_RESOURCES_URL + GET_IMAGE_PAGE);
117+
map.put("FAVICON_ICO", STATIC_RESOURCES_URL + FAVICON_ICO);
118+
map.put("MAIN_CSS", STATIC_RESOURCES_URL + MAIN_CSS);
119+
map.put("CATALOG_UTILS_JS", STATIC_RESOURCES_URL + CATALOG_UTILS_JS);
120+
121+
// Use CDN for external resources like frameworks
122+
map.put("BOOTSTRAP_CSS", BOOTSTRAP_CSS_CDN);
123+
map.put("BOOTSTRAP_JS", BOOTSTRAP_JS_CDN);
124+
map.put("JQUERY_JS", JQUERY_JS_CDN);
117125
}
118126

119127
return map;

src/main/java/ru/mystamps/web/support/spring/boot/ThymeleafViewResolverInitializingBean.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public void afterPropertiesSet() throws Exception {
6262
return;
6363
}
6464

65-
boolean useCdn = environment.acceptsProfiles("prod");
66-
viewResolver.setStaticVariables(Url.asMap(useCdn));
65+
boolean useSingleHost = !environment.acceptsProfiles("prod");
66+
viewResolver.setStaticVariables(Url.asMap(useSingleHost));
6767
}
6868

6969
}

0 commit comments

Comments
 (0)