Serve static content using docker + nginx + php-fpm The 2019 Stack Overflow Developer Survey Results Are InBlank Page: wordpress on nginx+php-fpmphpmyadmin having problems on nginx and php-fpm on RHEL 6nginx php5-fpm path_info urls and root locationNGINX don't parse .php5 as .phpDocker PHP-FPM & NGINXNginx php-fpm in docker - Getting 404sDocker - scaling nginx and php-fpm seperatelynginx, php-fpm, and subfoldersCodeIgniter nginx rewrite rules for i8ln URL's502 Bad Gateway with dockerized PHP-FPM
If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?
For what reasons would an animal species NOT cross a *horizontal* land bridge?
What to do when moving next to a bird sanctuary with a loosely-domesticated cat?
Deal with toxic manager when you can't quit
How to make Illustrator type tool selection automatically adapt with text length
What is this sharp, curved notch on my knife for?
Why not take a picture of a closer black hole?
Keeping a retro style to sci-fi spaceships?
Didn't get enough time to take a Coding Test - what to do now?
Did any laptop computers have a built-in 5 1/4 inch floppy drive?
Is Cinnamon a desktop environment or a window manager? (Or both?)
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
How can I have a shield and a way of attacking at distance at the same time?
Why are there uneven bright areas in this photo of black hole?
How to substitute curly brackets with round brackets in a grid of list
Is it okay to consider publishing in my first year of PhD?
Cooking pasta in a water boiler
Can a flute soloist sit?
How do you keep chess fun when your opponent constantly beats you?
What force causes entropy to increase?
Mortgage adviser recommends a longer term than necessary combined with overpayments
Correct punctuation for showing a character's confusion
Match Roman Numerals
How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?
Serve static content using docker + nginx + php-fpm
The 2019 Stack Overflow Developer Survey Results Are InBlank Page: wordpress on nginx+php-fpmphpmyadmin having problems on nginx and php-fpm on RHEL 6nginx php5-fpm path_info urls and root locationNGINX don't parse .php5 as .phpDocker PHP-FPM & NGINXNginx php-fpm in docker - Getting 404sDocker - scaling nginx and php-fpm seperatelynginx, php-fpm, and subfoldersCodeIgniter nginx rewrite rules for i8ln URL's502 Bad Gateway with dockerized PHP-FPM
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to configure a php webapp using docker. The idea is to run the app using php-fpm
in a standalone container and have another container that will run nginx. The idea for this setup is to use that same nginx container to proxy requests to other webapps that are already working on the same machine.
The problem is that I can't get nginx
to properly process static files (js, css, etc.), as the requests to those keep going to fpm
.
This is what the filesystem looks like:
/
├── Makefile
├── config
│ └── webapp.config
└── webapp
└── web
├── index.php
└── static.js
I'm running the whole thing using a Makefile
that looks like this (not interested in docker-compose
for this):
PWD:=$(shell pwd)
CONFIG:='/config'
WEBAPP:='/webapp'
run: | run-network run-webapp run-nginx
run-network:
docker network create internal-net
run-webapp:
docker run --rm
--name=webapp
--net=internal-net
--volume=$(PWD)$(WEBAPP):/var/www/webapp:ro
-p 9000:9000
php:5.6.22-fpm-alpine
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
-p 80:80
nginx:1.11.0-alpine
This is what my config/webapp.conf
looks like.
server
listen 80;
server_name webapp.domain.com;
# This is where the index.php file is located in the webapp container
# This folder will contain an index.php file and some static files that should be accessed directly
root /var/www/webapp/web;
location /
try_files $uri $uri/ @webapp;
location @webapp
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
Whatever action that needs to get processed using that index.php
file will work. However, static files won't be served, resulting in nasty 404
errors (as the php webapp doesn't really have routes configured for those). I believe nginx tries to load those from its own container filesystem, when they are actually in the webapp
container, failing back into @webapp
.
Is there a way I can configure nginx
to serve those files that reside in another container?
nginx php-fpm docker static-content
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I'm trying to configure a php webapp using docker. The idea is to run the app using php-fpm
in a standalone container and have another container that will run nginx. The idea for this setup is to use that same nginx container to proxy requests to other webapps that are already working on the same machine.
The problem is that I can't get nginx
to properly process static files (js, css, etc.), as the requests to those keep going to fpm
.
This is what the filesystem looks like:
/
├── Makefile
├── config
│ └── webapp.config
└── webapp
└── web
├── index.php
└── static.js
I'm running the whole thing using a Makefile
that looks like this (not interested in docker-compose
for this):
PWD:=$(shell pwd)
CONFIG:='/config'
WEBAPP:='/webapp'
run: | run-network run-webapp run-nginx
run-network:
docker network create internal-net
run-webapp:
docker run --rm
--name=webapp
--net=internal-net
--volume=$(PWD)$(WEBAPP):/var/www/webapp:ro
-p 9000:9000
php:5.6.22-fpm-alpine
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
-p 80:80
nginx:1.11.0-alpine
This is what my config/webapp.conf
looks like.
server
listen 80;
server_name webapp.domain.com;
# This is where the index.php file is located in the webapp container
# This folder will contain an index.php file and some static files that should be accessed directly
root /var/www/webapp/web;
location /
try_files $uri $uri/ @webapp;
location @webapp
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
Whatever action that needs to get processed using that index.php
file will work. However, static files won't be served, resulting in nasty 404
errors (as the php webapp doesn't really have routes configured for those). I believe nginx tries to load those from its own container filesystem, when they are actually in the webapp
container, failing back into @webapp
.
Is there a way I can configure nginx
to serve those files that reside in another container?
nginx php-fpm docker static-content
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
3
Are you using docker to isolate nginx from php applications while requiring that nginx has access to files within the php applications?
– Stefan Schmiedl
Jun 5 '16 at 19:09
I'm not sure I understand your comment... I'm using docker to manage my infrastructure. However, I'm not makingnginx
request files within the php application, I'm proxying tofpm
to do so and I do neednginx
to access static non-php files.
– ThisIsErico
Jun 5 '16 at 20:26
The files "reside in another container", i.e. not where nginx can see them, right?
– Stefan Schmiedl
Jun 5 '16 at 22:17
That's right @Stefan, they only get mounted as volumes in thewebapp
container, not in thenginx
one.
– ThisIsErico
Jun 6 '16 at 4:48
add a comment |
I'm trying to configure a php webapp using docker. The idea is to run the app using php-fpm
in a standalone container and have another container that will run nginx. The idea for this setup is to use that same nginx container to proxy requests to other webapps that are already working on the same machine.
The problem is that I can't get nginx
to properly process static files (js, css, etc.), as the requests to those keep going to fpm
.
This is what the filesystem looks like:
/
├── Makefile
├── config
│ └── webapp.config
└── webapp
└── web
├── index.php
└── static.js
I'm running the whole thing using a Makefile
that looks like this (not interested in docker-compose
for this):
PWD:=$(shell pwd)
CONFIG:='/config'
WEBAPP:='/webapp'
run: | run-network run-webapp run-nginx
run-network:
docker network create internal-net
run-webapp:
docker run --rm
--name=webapp
--net=internal-net
--volume=$(PWD)$(WEBAPP):/var/www/webapp:ro
-p 9000:9000
php:5.6.22-fpm-alpine
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
-p 80:80
nginx:1.11.0-alpine
This is what my config/webapp.conf
looks like.
server
listen 80;
server_name webapp.domain.com;
# This is where the index.php file is located in the webapp container
# This folder will contain an index.php file and some static files that should be accessed directly
root /var/www/webapp/web;
location /
try_files $uri $uri/ @webapp;
location @webapp
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
Whatever action that needs to get processed using that index.php
file will work. However, static files won't be served, resulting in nasty 404
errors (as the php webapp doesn't really have routes configured for those). I believe nginx tries to load those from its own container filesystem, when they are actually in the webapp
container, failing back into @webapp
.
Is there a way I can configure nginx
to serve those files that reside in another container?
nginx php-fpm docker static-content
I'm trying to configure a php webapp using docker. The idea is to run the app using php-fpm
in a standalone container and have another container that will run nginx. The idea for this setup is to use that same nginx container to proxy requests to other webapps that are already working on the same machine.
The problem is that I can't get nginx
to properly process static files (js, css, etc.), as the requests to those keep going to fpm
.
This is what the filesystem looks like:
/
├── Makefile
├── config
│ └── webapp.config
└── webapp
└── web
├── index.php
└── static.js
I'm running the whole thing using a Makefile
that looks like this (not interested in docker-compose
for this):
PWD:=$(shell pwd)
CONFIG:='/config'
WEBAPP:='/webapp'
run: | run-network run-webapp run-nginx
run-network:
docker network create internal-net
run-webapp:
docker run --rm
--name=webapp
--net=internal-net
--volume=$(PWD)$(WEBAPP):/var/www/webapp:ro
-p 9000:9000
php:5.6.22-fpm-alpine
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
-p 80:80
nginx:1.11.0-alpine
This is what my config/webapp.conf
looks like.
server
listen 80;
server_name webapp.domain.com;
# This is where the index.php file is located in the webapp container
# This folder will contain an index.php file and some static files that should be accessed directly
root /var/www/webapp/web;
location /
try_files $uri $uri/ @webapp;
location @webapp
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
Whatever action that needs to get processed using that index.php
file will work. However, static files won't be served, resulting in nasty 404
errors (as the php webapp doesn't really have routes configured for those). I believe nginx tries to load those from its own container filesystem, when they are actually in the webapp
container, failing back into @webapp
.
Is there a way I can configure nginx
to serve those files that reside in another container?
nginx php-fpm docker static-content
nginx php-fpm docker static-content
edited Mar 7 at 14:57
Chad
1134
1134
asked Jun 5 '16 at 18:14
ThisIsEricoThisIsErico
14613
14613
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
3
Are you using docker to isolate nginx from php applications while requiring that nginx has access to files within the php applications?
– Stefan Schmiedl
Jun 5 '16 at 19:09
I'm not sure I understand your comment... I'm using docker to manage my infrastructure. However, I'm not makingnginx
request files within the php application, I'm proxying tofpm
to do so and I do neednginx
to access static non-php files.
– ThisIsErico
Jun 5 '16 at 20:26
The files "reside in another container", i.e. not where nginx can see them, right?
– Stefan Schmiedl
Jun 5 '16 at 22:17
That's right @Stefan, they only get mounted as volumes in thewebapp
container, not in thenginx
one.
– ThisIsErico
Jun 6 '16 at 4:48
add a comment |
3
Are you using docker to isolate nginx from php applications while requiring that nginx has access to files within the php applications?
– Stefan Schmiedl
Jun 5 '16 at 19:09
I'm not sure I understand your comment... I'm using docker to manage my infrastructure. However, I'm not makingnginx
request files within the php application, I'm proxying tofpm
to do so and I do neednginx
to access static non-php files.
– ThisIsErico
Jun 5 '16 at 20:26
The files "reside in another container", i.e. not where nginx can see them, right?
– Stefan Schmiedl
Jun 5 '16 at 22:17
That's right @Stefan, they only get mounted as volumes in thewebapp
container, not in thenginx
one.
– ThisIsErico
Jun 6 '16 at 4:48
3
3
Are you using docker to isolate nginx from php applications while requiring that nginx has access to files within the php applications?
– Stefan Schmiedl
Jun 5 '16 at 19:09
Are you using docker to isolate nginx from php applications while requiring that nginx has access to files within the php applications?
– Stefan Schmiedl
Jun 5 '16 at 19:09
I'm not sure I understand your comment... I'm using docker to manage my infrastructure. However, I'm not making
nginx
request files within the php application, I'm proxying to fpm
to do so and I do need nginx
to access static non-php files.– ThisIsErico
Jun 5 '16 at 20:26
I'm not sure I understand your comment... I'm using docker to manage my infrastructure. However, I'm not making
nginx
request files within the php application, I'm proxying to fpm
to do so and I do need nginx
to access static non-php files.– ThisIsErico
Jun 5 '16 at 20:26
The files "reside in another container", i.e. not where nginx can see them, right?
– Stefan Schmiedl
Jun 5 '16 at 22:17
The files "reside in another container", i.e. not where nginx can see them, right?
– Stefan Schmiedl
Jun 5 '16 at 22:17
That's right @Stefan, they only get mounted as volumes in the
webapp
container, not in the nginx
one.– ThisIsErico
Jun 6 '16 at 4:48
That's right @Stefan, they only get mounted as volumes in the
webapp
container, not in the nginx
one.– ThisIsErico
Jun 6 '16 at 4:48
add a comment |
3 Answers
3
active
oldest
votes
I managed to solve the problem by mounting the webapp
volume in the nginx
container.
This is what the run-nginx
job looks like now:
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
--volume=$(PWD)$(WEBAPP)/web:/var/www/webapp/web:ro
-p 80:80
nginx:1.11.0-alpine
And this is the webapp.conf
file, that will try to load the static files from the container and, if that's not possible will proxy the request to the fpm
worker:
server png)
try_files $uri $uri/;
location /
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
However, I'd like to know if there's a better way to do so instead of sharing the same volume twice.
Thanks a lot!
4
Given your separation of nginx and php into different containers, I don't think so. You need the data in two different locations, you have to provide it twice. I'm very curious, too, if somebody comes up with a better idea.
– Stefan Schmiedl
Jun 6 '16 at 18:21
add a comment |
Maybe this could be achieved using NFS
One docker container running NFS could be made where the code resides, which could be linked to the containers running nginx and php. The files would be stored in one container only.
This could provide another layer of isolation as well.
add a comment |
I have two suggested options: The first one is to put your static assets in e.g. /static and instruct nginx to call a different backend service for those. Steps:
1) Update your websites to point to /static/* for any static assets, so e.g. /styles.css becomes /static/styles.css
2) Put your assets either in a separate container served by maybe another nginx (so you can reuse the container for several sites)
3) Edit nginx.conf to send all requests to /static/* to the new container:
location /static/
proxy_pass http://static-container;
The second option is to just move your static assets to a CDN, so you just need to update your website to load each static asset from an external URL (https://cdnwebsite/asdsadasda/styles.css instead of /styles.css or /static/styles.css)
The second option has several advantages over the others, mainly around performance. A CDN will serve those faster and you're also working around the simultaneous connection limit a browser can make to each FQDN, so your page might load faster due to more simultaneous connections being used to load your site.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f782103%2fserve-static-content-using-docker-nginx-php-fpm%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I managed to solve the problem by mounting the webapp
volume in the nginx
container.
This is what the run-nginx
job looks like now:
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
--volume=$(PWD)$(WEBAPP)/web:/var/www/webapp/web:ro
-p 80:80
nginx:1.11.0-alpine
And this is the webapp.conf
file, that will try to load the static files from the container and, if that's not possible will proxy the request to the fpm
worker:
server png)
try_files $uri $uri/;
location /
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
However, I'd like to know if there's a better way to do so instead of sharing the same volume twice.
Thanks a lot!
4
Given your separation of nginx and php into different containers, I don't think so. You need the data in two different locations, you have to provide it twice. I'm very curious, too, if somebody comes up with a better idea.
– Stefan Schmiedl
Jun 6 '16 at 18:21
add a comment |
I managed to solve the problem by mounting the webapp
volume in the nginx
container.
This is what the run-nginx
job looks like now:
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
--volume=$(PWD)$(WEBAPP)/web:/var/www/webapp/web:ro
-p 80:80
nginx:1.11.0-alpine
And this is the webapp.conf
file, that will try to load the static files from the container and, if that's not possible will proxy the request to the fpm
worker:
server png)
try_files $uri $uri/;
location /
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
However, I'd like to know if there's a better way to do so instead of sharing the same volume twice.
Thanks a lot!
4
Given your separation of nginx and php into different containers, I don't think so. You need the data in two different locations, you have to provide it twice. I'm very curious, too, if somebody comes up with a better idea.
– Stefan Schmiedl
Jun 6 '16 at 18:21
add a comment |
I managed to solve the problem by mounting the webapp
volume in the nginx
container.
This is what the run-nginx
job looks like now:
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
--volume=$(PWD)$(WEBAPP)/web:/var/www/webapp/web:ro
-p 80:80
nginx:1.11.0-alpine
And this is the webapp.conf
file, that will try to load the static files from the container and, if that's not possible will proxy the request to the fpm
worker:
server png)
try_files $uri $uri/;
location /
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
However, I'd like to know if there's a better way to do so instead of sharing the same volume twice.
Thanks a lot!
I managed to solve the problem by mounting the webapp
volume in the nginx
container.
This is what the run-nginx
job looks like now:
run-nginx:
docker run --rm
--name=nginx
--net=internal-net
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro
--volume=$(PWD)$(WEBAPP)/web:/var/www/webapp/web:ro
-p 80:80
nginx:1.11.0-alpine
And this is the webapp.conf
file, that will try to load the static files from the container and, if that's not possible will proxy the request to the fpm
worker:
server png)
try_files $uri $uri/;
location /
rewrite ^(.*)$ /index.php$1 last;
location ~ ^/index.php(/
However, I'd like to know if there's a better way to do so instead of sharing the same volume twice.
Thanks a lot!
answered Jun 6 '16 at 16:40
ThisIsEricoThisIsErico
14613
14613
4
Given your separation of nginx and php into different containers, I don't think so. You need the data in two different locations, you have to provide it twice. I'm very curious, too, if somebody comes up with a better idea.
– Stefan Schmiedl
Jun 6 '16 at 18:21
add a comment |
4
Given your separation of nginx and php into different containers, I don't think so. You need the data in two different locations, you have to provide it twice. I'm very curious, too, if somebody comes up with a better idea.
– Stefan Schmiedl
Jun 6 '16 at 18:21
4
4
Given your separation of nginx and php into different containers, I don't think so. You need the data in two different locations, you have to provide it twice. I'm very curious, too, if somebody comes up with a better idea.
– Stefan Schmiedl
Jun 6 '16 at 18:21
Given your separation of nginx and php into different containers, I don't think so. You need the data in two different locations, you have to provide it twice. I'm very curious, too, if somebody comes up with a better idea.
– Stefan Schmiedl
Jun 6 '16 at 18:21
add a comment |
Maybe this could be achieved using NFS
One docker container running NFS could be made where the code resides, which could be linked to the containers running nginx and php. The files would be stored in one container only.
This could provide another layer of isolation as well.
add a comment |
Maybe this could be achieved using NFS
One docker container running NFS could be made where the code resides, which could be linked to the containers running nginx and php. The files would be stored in one container only.
This could provide another layer of isolation as well.
add a comment |
Maybe this could be achieved using NFS
One docker container running NFS could be made where the code resides, which could be linked to the containers running nginx and php. The files would be stored in one container only.
This could provide another layer of isolation as well.
Maybe this could be achieved using NFS
One docker container running NFS could be made where the code resides, which could be linked to the containers running nginx and php. The files would be stored in one container only.
This could provide another layer of isolation as well.
answered Aug 28 '16 at 17:28
MagarusuMagarusu
101
101
add a comment |
add a comment |
I have two suggested options: The first one is to put your static assets in e.g. /static and instruct nginx to call a different backend service for those. Steps:
1) Update your websites to point to /static/* for any static assets, so e.g. /styles.css becomes /static/styles.css
2) Put your assets either in a separate container served by maybe another nginx (so you can reuse the container for several sites)
3) Edit nginx.conf to send all requests to /static/* to the new container:
location /static/
proxy_pass http://static-container;
The second option is to just move your static assets to a CDN, so you just need to update your website to load each static asset from an external URL (https://cdnwebsite/asdsadasda/styles.css instead of /styles.css or /static/styles.css)
The second option has several advantages over the others, mainly around performance. A CDN will serve those faster and you're also working around the simultaneous connection limit a browser can make to each FQDN, so your page might load faster due to more simultaneous connections being used to load your site.
add a comment |
I have two suggested options: The first one is to put your static assets in e.g. /static and instruct nginx to call a different backend service for those. Steps:
1) Update your websites to point to /static/* for any static assets, so e.g. /styles.css becomes /static/styles.css
2) Put your assets either in a separate container served by maybe another nginx (so you can reuse the container for several sites)
3) Edit nginx.conf to send all requests to /static/* to the new container:
location /static/
proxy_pass http://static-container;
The second option is to just move your static assets to a CDN, so you just need to update your website to load each static asset from an external URL (https://cdnwebsite/asdsadasda/styles.css instead of /styles.css or /static/styles.css)
The second option has several advantages over the others, mainly around performance. A CDN will serve those faster and you're also working around the simultaneous connection limit a browser can make to each FQDN, so your page might load faster due to more simultaneous connections being used to load your site.
add a comment |
I have two suggested options: The first one is to put your static assets in e.g. /static and instruct nginx to call a different backend service for those. Steps:
1) Update your websites to point to /static/* for any static assets, so e.g. /styles.css becomes /static/styles.css
2) Put your assets either in a separate container served by maybe another nginx (so you can reuse the container for several sites)
3) Edit nginx.conf to send all requests to /static/* to the new container:
location /static/
proxy_pass http://static-container;
The second option is to just move your static assets to a CDN, so you just need to update your website to load each static asset from an external URL (https://cdnwebsite/asdsadasda/styles.css instead of /styles.css or /static/styles.css)
The second option has several advantages over the others, mainly around performance. A CDN will serve those faster and you're also working around the simultaneous connection limit a browser can make to each FQDN, so your page might load faster due to more simultaneous connections being used to load your site.
I have two suggested options: The first one is to put your static assets in e.g. /static and instruct nginx to call a different backend service for those. Steps:
1) Update your websites to point to /static/* for any static assets, so e.g. /styles.css becomes /static/styles.css
2) Put your assets either in a separate container served by maybe another nginx (so you can reuse the container for several sites)
3) Edit nginx.conf to send all requests to /static/* to the new container:
location /static/
proxy_pass http://static-container;
The second option is to just move your static assets to a CDN, so you just need to update your website to load each static asset from an external URL (https://cdnwebsite/asdsadasda/styles.css instead of /styles.css or /static/styles.css)
The second option has several advantages over the others, mainly around performance. A CDN will serve those faster and you're also working around the simultaneous connection limit a browser can make to each FQDN, so your page might load faster due to more simultaneous connections being used to load your site.
answered Mar 7 at 15:42
Pedro PerezPedro Perez
2,865167
2,865167
add a comment |
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f782103%2fserve-static-content-using-docker-nginx-php-fpm%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Are you using docker to isolate nginx from php applications while requiring that nginx has access to files within the php applications?
– Stefan Schmiedl
Jun 5 '16 at 19:09
I'm not sure I understand your comment... I'm using docker to manage my infrastructure. However, I'm not making
nginx
request files within the php application, I'm proxying tofpm
to do so and I do neednginx
to access static non-php files.– ThisIsErico
Jun 5 '16 at 20:26
The files "reside in another container", i.e. not where nginx can see them, right?
– Stefan Schmiedl
Jun 5 '16 at 22:17
That's right @Stefan, they only get mounted as volumes in the
webapp
container, not in thenginx
one.– ThisIsErico
Jun 6 '16 at 4:48