Nginx: Difference between revisions

Jump to navigation Jump to search
347 bytes added ,  24 February 2022
Add flask
imported>Sjwhitak
(More links)
imported>Sjwhitak
(Add flask)
 
To use a reverse proxy, remember the port your service is running on, and then add it into your nginx configuration:
<pre>
 
server {
server_name example.com;
listen 80;
listen [::]:80;
location / {
proxy_pass http://localhost:8080;
}
}
</pre>
This is a simple set up for an executable running on port 8080. I would ensure your firewall does not allow outside access to these ports, else anyone can directly access the service without nginx's protection.
 
Suppose you want multiple webapps, then:
<pre>
 
server {
server_name example.com;
listen 80;
listen [::]:80;
location / {
proxy_pass http://localhost:8080; # Homepage
}
location /tags {
proxy_pass http://localhost:8081; # Cool web app 1
}
location /wiki {
proxy_pass http://localhost:8082; # Cool web app 2
}
...
}
</pre>
If someone navigates to <code>example.com</code>, nginx serves them data from the service running on port <code>8080</code>.
 
 
This shows the purpose of the [https://nginx.org/en/docs/http/ngx_http_core_module.html#location <code>location</code>] directive.
 
=== flask ===
 
Running a simple flask server can be done in Python:
<pre>
from flask import Flask
app = Flask(__name__)
 
@app.route('/flask')
def main():
return "Flask app"
 
app.run(port=9999)
</pre>
 
And in nginx, the configuration should be:
<pre>
server {
location /flask {
proxy_pass http://localhost:9999;
}
}
</pre>
 
== <code>fastcgi</code> ==
Anonymous user

Navigation menu