Authorize with Spotify using NginxNode.js + Nginx - What now?what's wrong with this configuration for nginx as reverse proxy for node.js?Express - req.ip returns 127.0.0.1Websocket rails (Faye-websocket) handshake error code 200 - AWS ElasticBeanstalk, nginx, pumaMulti Docker container with PHP7 fpm and nginxDocker, Nginx and PHP7: ERROR 111 Connection refused while connecting to upstreamDocker nginx reverse proxy returns 502 bad gateway “connection refused while connecting to upstream”docker nginx ERR_NAME_NOT_RESOLVEDAiohttp and NGINX running in DockerAuthorize Spotify using React and Nginx in Docker
Is there an idiom that means that you are in a very strong negotiation position in a negotiation?
What pc resources are used when bruteforcing?
Unary Enumeration
nginx conf: http2 module not working in Chrome in ubuntu 18.04
Is being an extrovert a necessary condition to be a manager?
Is it OK to look at the list of played moves during the game to determine the status of the 50 move rule?
Nunc est bibendum: gerund or gerundive?
Writing "hahaha" versus describing the laugh
Ratings matrix plot
Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?
What is the winged creature on the back of the Mordenkainen's Tome of Foes book?
csname in newenviroment
How to make Flex Markers appear in Logic Pro X?
Can diplomats be allowed on the flight deck of a commercial European airline?
If a character has cast the Fly spell on themselves, can they "hand off" to the Levitate spell without interruption?
How does the Earth's center produce heat?
Is it normal to "extract a paper" from a master thesis?
Download app bundles from App Store to run on iOS Emulator on Mac
Meaning of "half-crown enclosure"
Palindromic Pan digital Special Square wants you to reveal its Root
How many wires should be in a new thermostat cable?
Salesforce bug enabled "Modify All"
What all did Brienn write about Jaime?
Why A=2 and B=1 in the call signs for Spirit and Opportunity?
Authorize with Spotify using Nginx
Node.js + Nginx - What now?what's wrong with this configuration for nginx as reverse proxy for node.js?Express - req.ip returns 127.0.0.1Websocket rails (Faye-websocket) handshake error code 200 - AWS ElasticBeanstalk, nginx, pumaMulti Docker container with PHP7 fpm and nginxDocker, Nginx and PHP7: ERROR 111 Connection refused while connecting to upstreamDocker nginx reverse proxy returns 502 bad gateway “connection refused while connecting to upstream”docker nginx ERR_NAME_NOT_RESOLVEDAiohttp and NGINX running in DockerAuthorize Spotify using React and Nginx in Docker
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a docker app running three services:
client --> react frontend
web --- > flask backend
nginx ->- a reverse proxy for both
This is the (simplified) project structure:
docker-compose-dev.yml
services/
client/
src/
app.jsx
components/
spotify-auth.js
Spotify.jsx
nginx/
dev.conf
web/
Here is where I defined exposed ports at built time:
docker-compose-dev.yml
web:
build:
context: ./services/web
dockerfile: Dockerfile-dev
volumes:
- './services/web:/usr/src/app'
ports:
- 5001:5000 <----------------
environment:
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
depends_on:
- web-db
nginx:
build:
context: ./services/nginx
dockerfile: Dockerfile-dev
restart: always
ports:
- 80:80 <----------------
- 8888:8888 <----------------
depends_on:
- web
- client
client:
build:
context: ./services/client
dockerfile: Dockerfile-dev
volumes:
- './services/client:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- 3007:3000 <----------------
environment:
- NODE_ENV=development
- REACT_APP_WEB_SERVICE_URL=$REACT_APP_WEB_SERVICE_URL
depends_on:
- web
REDIRECT
Client
service, on its turn, needs to authenticate with Spotify
, which requires a Redirect URI
, whitelisted at https://developer.spotify.com. For mine, I have several options:
This is my nginx file, where I try to organize the right ports:
dev.conf
server
listen 80;
listen 8888;
location / // frontend at localhost:3000
proxy_pass http://client:3000;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
location /users // backend at localhost:5000
proxy_pass http://web:5000;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
location /auth # this authentication is for the app, not spotify
proxy_pass http://web:5000;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
Finally, there's my js
and jsx
files used to:
- Authenticate with Spotify -----> Implicit Grant
- Redirect my app back to
localhost
, or'/'
spotify-auth.js
export const stateKey = 'spotify_auth_state';
export const client_id = 'my_client_id'; // Your client id
export const redirect_uri = 'http://localhost:3000'; // my redirect uri
//export const redirect_uri = 'http://localhost:8888'; // my second try for uri
export const scope ='user-read-private user-read-email user-read-playback-state playlist-modify-public playlist-modify-private';
Spotify.jsx
class SpotifyAuth extends Component
constructor (props)
super(props);
this.state =
isAuthenticatedWithSpotify: false
;
this.state.handleRedirect = this.handleRedirect.bind(this);
this.loginSpotifyUser = this.loginSpotifyUser.bind(this);
;
function generateRandomString(length)
let text = '';
const possible =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
getHashParams()
const hashParams = ;
const r = /([^&;=]+)=?([^&;]*)/g;
const q = window.location.hash.substring(1);
let e = r.exec(q);
while (e)
hashParams[e[1]] = decodeURIComponent(e[2]);
e = r.exec(q);
return hashParams;
componentDidMount() state !== storedState))
alert('There was an error during the authentication');
else
localStorage.removeItem(stateKey);
// DO STUFF WITH ACCESS TOKEN HERE -- send ajax to backend routes
;
handleRedirect()
const state = generateRandomString(16);
localStorage.setItem(stateKey, state);
let url = 'https://accounts.spotify.com/authorize';
url += '?response_type=token';
url += '&client_id=' + encodeURIComponent(client_id);
url += '&scope=' + encodeURIComponent(scope);
url += '&redirect_uri=' + encodeURIComponent(redirect_uri);
url += '&state=' + encodeURIComponent(state);
window.location = url;
// post data to backend
const url = `$process.env.REACT_APP_WEB_SERVICE_URL/auth/spotify`;
axios.post(url, data)
.then((res) =>
this.loginSpotifyUser(res.data.auth_token);
)
.catch((err) => console.log(err); );
;
loginSpotifyUser(token)
window.localStorage.setItem('spotifyAuthToken', token);
this.setState( isAuthenticatedWithSpotify: true );
this.props.createMessage('Welcome to Spotify', 'success');
;
render()
return (
<div className="button_container">
<button className="sp_button" onClick=this.handleRedirect>
<strong>CONNECT YOUR SPOTIFY ACCOUNT</strong>
</button>
</div>
)
}
export default SpotifyAuth;
and render, like so:
App.jsx
render() 172.21.0.1 - - [27/Mar/2019:03:58:56 +0000] "GET /static/js/bundle.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
nginx_1
export default SpotifyAuth;
and render, like so:
App.jsx
render() * Debug mode: on
web_1
export default SpotifyAuth;
and render, like so:
App.jsx
render() Local: http://localhost:3000/
web_1
export default SpotifyAuth;
and render, like so:
App.jsx
render() {
return (
<div>
<Switch>
<Route exact path='/' render=() => (
<SpotifyAuth/>
)
/>
</Switch>
</div>
ERROR:
After all of this is set and run, I get:
INVALID_CLIENT: Invalid redirect URI
LOGS:
Before building, I export this env
variable:
$ export REACT_APP_WEB_SERVICE_URL=http://localhost
After services are built, I get the logs
:
client_1 | You can now view client in the browser.
client_1 |
client_1 | Local: http://localhost:3000/
web_1 | * Environment: development
web_1 | * Debug mode: on
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
nginx_1 | 172.21.0.1 - - [27/Mar/2019:03:58:56 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
nginx_1 | 172.21.0.1 - - [27/Mar/2019:03:58:56 +0000] "GET /static/js/0.chunk.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
nginx_1 | 172.21.0.1 - - [27/Mar/2019:03:58:56 +0000] "GET /static/js/bundle.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
nginx_1 | 172.21.0.1 - - [27/Mar/2019:03:58:56 +0000] "GET /static/js/main.chunk.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
NOTE:
If I try and use localhost:8888
at Spotify.jsx
, the app manages to authenticate with Spotify
, but then all locations start with localhost:8888/auth/login
and so on, which is not desired.
QUESTION:
Why won't localhost:3000
, my client
, work as redirect uri
? What am I missing?
Is this the most reliable way of authenticating with Spotify
on a docker
project like this one?
reactjs docker nginx spotify
reactjs docker nginx spotify
edited Mar 28 at 2:24
data_garden
asked Mar 23 at 1:58
data_gardendata_garden
1,45363260
1,45363260
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your issue:
Your docker-compose.yml
has the client
container configured with port mapping 3007:3000
. Notice that Docker-compose port mapping is host:container
(Compose file reference), this means that port 3007 of your host is mapping to port 3000 of the container.
This way, you're trying to connect to a port of your container that is not available in your host, although nginx
is happy with it because it is on the same network as your client, so it can reach it and redirect the request.
If that is the case:
http://localhost:3000
doesn't work, because it is closed in the host side.http://localhost:3007
opens your client, because it is redirected to your client container, but you won't be able to use the Spotify auth there unless you whitelisted this URL and changed yourredirect_uri
.http://localhost:8888
opens your client, because you havenginx
setup as reverse proxy, and it can access the client port 3000 because it's on the same network.
Your solution:
Your solution is changing the docker-compose
so client is mapped at ports 3000:3000
. Then Spotify auth should be fine as the ports are open and the URLs are properly configured.
Extra:
Regarding your request for opinion on the design, nginx
feels a bit underused by your design. If you set up a reverse proxy, is so that the services you're redirecting to are hidden in a secure network that cannot be accessed from outside. This way you can for example configure SSL on nginx
and forget about HTTPs on the rest of the services. However, if such services can be accessed from other ports, it is of no use that you make this kind of configuration.
On a production setup, you would want to close the client
and web
ports from your docker-compose (literally, remove the port mapping. Nginx will have no problems accessing your containers as it's on the same network, unlike your host) and leave only nginx
exposed to the real world.
You might also want to setup a rewrite
rule where the client and server hang on http://localhost/client
, http://localhost/server
addresses respectively, but nginx rewrites the request and proxies it to the appropiate container so the containers actually see a request coming to http://localhost:3000/
. You can see an example on how to configure all this at Stack Exchange - Nginx reverse proxy + URL rewrite.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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%2fstackoverflow.com%2fquestions%2f55309911%2fauthorize-with-spotify-using-nginx%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your issue:
Your docker-compose.yml
has the client
container configured with port mapping 3007:3000
. Notice that Docker-compose port mapping is host:container
(Compose file reference), this means that port 3007 of your host is mapping to port 3000 of the container.
This way, you're trying to connect to a port of your container that is not available in your host, although nginx
is happy with it because it is on the same network as your client, so it can reach it and redirect the request.
If that is the case:
http://localhost:3000
doesn't work, because it is closed in the host side.http://localhost:3007
opens your client, because it is redirected to your client container, but you won't be able to use the Spotify auth there unless you whitelisted this URL and changed yourredirect_uri
.http://localhost:8888
opens your client, because you havenginx
setup as reverse proxy, and it can access the client port 3000 because it's on the same network.
Your solution:
Your solution is changing the docker-compose
so client is mapped at ports 3000:3000
. Then Spotify auth should be fine as the ports are open and the URLs are properly configured.
Extra:
Regarding your request for opinion on the design, nginx
feels a bit underused by your design. If you set up a reverse proxy, is so that the services you're redirecting to are hidden in a secure network that cannot be accessed from outside. This way you can for example configure SSL on nginx
and forget about HTTPs on the rest of the services. However, if such services can be accessed from other ports, it is of no use that you make this kind of configuration.
On a production setup, you would want to close the client
and web
ports from your docker-compose (literally, remove the port mapping. Nginx will have no problems accessing your containers as it's on the same network, unlike your host) and leave only nginx
exposed to the real world.
You might also want to setup a rewrite
rule where the client and server hang on http://localhost/client
, http://localhost/server
addresses respectively, but nginx rewrites the request and proxies it to the appropiate container so the containers actually see a request coming to http://localhost:3000/
. You can see an example on how to configure all this at Stack Exchange - Nginx reverse proxy + URL rewrite.
add a comment |
Your issue:
Your docker-compose.yml
has the client
container configured with port mapping 3007:3000
. Notice that Docker-compose port mapping is host:container
(Compose file reference), this means that port 3007 of your host is mapping to port 3000 of the container.
This way, you're trying to connect to a port of your container that is not available in your host, although nginx
is happy with it because it is on the same network as your client, so it can reach it and redirect the request.
If that is the case:
http://localhost:3000
doesn't work, because it is closed in the host side.http://localhost:3007
opens your client, because it is redirected to your client container, but you won't be able to use the Spotify auth there unless you whitelisted this URL and changed yourredirect_uri
.http://localhost:8888
opens your client, because you havenginx
setup as reverse proxy, and it can access the client port 3000 because it's on the same network.
Your solution:
Your solution is changing the docker-compose
so client is mapped at ports 3000:3000
. Then Spotify auth should be fine as the ports are open and the URLs are properly configured.
Extra:
Regarding your request for opinion on the design, nginx
feels a bit underused by your design. If you set up a reverse proxy, is so that the services you're redirecting to are hidden in a secure network that cannot be accessed from outside. This way you can for example configure SSL on nginx
and forget about HTTPs on the rest of the services. However, if such services can be accessed from other ports, it is of no use that you make this kind of configuration.
On a production setup, you would want to close the client
and web
ports from your docker-compose (literally, remove the port mapping. Nginx will have no problems accessing your containers as it's on the same network, unlike your host) and leave only nginx
exposed to the real world.
You might also want to setup a rewrite
rule where the client and server hang on http://localhost/client
, http://localhost/server
addresses respectively, but nginx rewrites the request and proxies it to the appropiate container so the containers actually see a request coming to http://localhost:3000/
. You can see an example on how to configure all this at Stack Exchange - Nginx reverse proxy + URL rewrite.
add a comment |
Your issue:
Your docker-compose.yml
has the client
container configured with port mapping 3007:3000
. Notice that Docker-compose port mapping is host:container
(Compose file reference), this means that port 3007 of your host is mapping to port 3000 of the container.
This way, you're trying to connect to a port of your container that is not available in your host, although nginx
is happy with it because it is on the same network as your client, so it can reach it and redirect the request.
If that is the case:
http://localhost:3000
doesn't work, because it is closed in the host side.http://localhost:3007
opens your client, because it is redirected to your client container, but you won't be able to use the Spotify auth there unless you whitelisted this URL and changed yourredirect_uri
.http://localhost:8888
opens your client, because you havenginx
setup as reverse proxy, and it can access the client port 3000 because it's on the same network.
Your solution:
Your solution is changing the docker-compose
so client is mapped at ports 3000:3000
. Then Spotify auth should be fine as the ports are open and the URLs are properly configured.
Extra:
Regarding your request for opinion on the design, nginx
feels a bit underused by your design. If you set up a reverse proxy, is so that the services you're redirecting to are hidden in a secure network that cannot be accessed from outside. This way you can for example configure SSL on nginx
and forget about HTTPs on the rest of the services. However, if such services can be accessed from other ports, it is of no use that you make this kind of configuration.
On a production setup, you would want to close the client
and web
ports from your docker-compose (literally, remove the port mapping. Nginx will have no problems accessing your containers as it's on the same network, unlike your host) and leave only nginx
exposed to the real world.
You might also want to setup a rewrite
rule where the client and server hang on http://localhost/client
, http://localhost/server
addresses respectively, but nginx rewrites the request and proxies it to the appropiate container so the containers actually see a request coming to http://localhost:3000/
. You can see an example on how to configure all this at Stack Exchange - Nginx reverse proxy + URL rewrite.
Your issue:
Your docker-compose.yml
has the client
container configured with port mapping 3007:3000
. Notice that Docker-compose port mapping is host:container
(Compose file reference), this means that port 3007 of your host is mapping to port 3000 of the container.
This way, you're trying to connect to a port of your container that is not available in your host, although nginx
is happy with it because it is on the same network as your client, so it can reach it and redirect the request.
If that is the case:
http://localhost:3000
doesn't work, because it is closed in the host side.http://localhost:3007
opens your client, because it is redirected to your client container, but you won't be able to use the Spotify auth there unless you whitelisted this URL and changed yourredirect_uri
.http://localhost:8888
opens your client, because you havenginx
setup as reverse proxy, and it can access the client port 3000 because it's on the same network.
Your solution:
Your solution is changing the docker-compose
so client is mapped at ports 3000:3000
. Then Spotify auth should be fine as the ports are open and the URLs are properly configured.
Extra:
Regarding your request for opinion on the design, nginx
feels a bit underused by your design. If you set up a reverse proxy, is so that the services you're redirecting to are hidden in a secure network that cannot be accessed from outside. This way you can for example configure SSL on nginx
and forget about HTTPs on the rest of the services. However, if such services can be accessed from other ports, it is of no use that you make this kind of configuration.
On a production setup, you would want to close the client
and web
ports from your docker-compose (literally, remove the port mapping. Nginx will have no problems accessing your containers as it's on the same network, unlike your host) and leave only nginx
exposed to the real world.
You might also want to setup a rewrite
rule where the client and server hang on http://localhost/client
, http://localhost/server
addresses respectively, but nginx rewrites the request and proxies it to the appropiate container so the containers actually see a request coming to http://localhost:3000/
. You can see an example on how to configure all this at Stack Exchange - Nginx reverse proxy + URL rewrite.
answered Mar 30 at 23:12
Marc SancesMarc Sances
40829
40829
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55309911%2fauthorize-with-spotify-using-nginx%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