<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ekwinder Saini on Medium]]></title>
        <description><![CDATA[Stories by Ekwinder Saini on Medium]]></description>
        <link>https://medium.com/@ekwinder?source=rss-86ecb96b6e5e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yIn1SvU5MCb4BM1jWjqhDA.jpeg</url>
            <title>Stories by Ekwinder Saini on Medium</title>
            <link>https://medium.com/@ekwinder?source=rss-86ecb96b6e5e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 02:33:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ekwinder/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Setting up uWSGI with Nginx on MacOS for Python web-apps]]></title>
            <link>https://medium.com/@ekwinder/setting-up-uwsgi-with-nginx-on-macos-for-python-web-apps-25edf4edab19?source=rss-86ecb96b6e5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/25edf4edab19</guid>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[uwsgi]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ekwinder Saini]]></dc:creator>
            <pubDate>Sat, 09 Feb 2019 13:20:56 GMT</pubDate>
            <atom:updated>2019-02-09T13:22:22.203Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*468fipoZ4aX3lk2NR3HG0A.png" /></figure><p>I have always used the default app servers provided by the respective framework while testing Python web-apps locally on my MacBook. Whenever I needed to work with Nginx: to setup a proxy server or create a load balancer, I would do that on an Linux EC2 instance. Couple of days ago, I wanted to setup a uWSGI app behind an Nginx server on my local MacOS machine. I assumed it to be fairly simple until I realised how many hurdles there were to accomplish something supposed to be so straight forward.</p><p>First of all there is no consolidated source online which could help people with limited or no knowledge of Nginx or uWSGI for this. You have to spend considerable amount of time searching multiple sources on how to create a new Nginx config on MacOS and how to proxy uWSGI app requests through the Nginx server. Needless to say it took me a few hours to do something that should be simple and uncomplicated. So I created a consolidate version of what all you might need to get it running.</p><p>If you don’t have Nginx installed, you can install that easily with —</p><pre>brew install nginx</pre><p>To check if nginx is installed correctly, run —</p><pre>sudo nginx</pre><p>and navigate to <a href="http://localhost:8080/">http://localhost:8080</a> . You should see a <strong><em>Welcome to Nginx</em></strong><em> </em>page. It means that Nginx was installed correctly and is running right now. We will come back to Nginx later when we need to update the configuration.</p><p>Let’s get started with the Python web-app now. We will need to install uWSGI first. It would be better to create a virtualenv and install uWSGI in it. Create an empty directory for the project. Name it anything such as myapp and navigate into that directory. In my case the path is /Users/Ekwinder/myapp/.</p><p>Install uWSGI using pip inside the environment —</p><pre>pip install uwsgi</pre><p>Run uwsgi --version to check if it was installed correctly. It should return the version number.</p><p>Let’s create the simplest uWSGI application. Create a file named uwsgi.py and add the following to it —</p><pre>def application(env, start_response):<br>    start_response(&#39;200 OK&#39;, [(&#39;Content-Type&#39;,&#39;text/html&#39;)])<br>    return [b&quot;Hello World&quot;]</pre><p>By default the uWSGI python loader searches for a callable; named application.</p><p>You could run uWSGI server on port 8081 as -</p><pre>uwsgi --http :8081 --wsgi-file wsgi.py<br></pre><p>or you could also use a config file in --wsgi-file to get your configuration from. Create a file called myapp.ini and add —</p><pre>[uwsgi]<br># -------------<br># Settings:<br># key = value<br># Comments &gt;&gt; #<br># -------------</pre><pre># socket = [addr:port]<br>http = 127.0.0.1:8081</pre><pre># Base application directory<br># chdir = /full/path<br>chdir  = /Users/Ekwinder/myapp</pre><pre># WSGI module and callable<br># module = [wsgi_module_name]:[application_callable_name]<br>module = wsgi:application</pre><pre># master = [master process (true of false)]<br>master = true</pre><pre># processes = [number of processes]<br>processes = 5</pre><p>Change the directory in chdir to your project directory. To pick config from this file run —</p><pre>uwsgi myapp.ini</pre><p>Go to <a href="http://127.0.0.1:8081/">127.0.0.1:8081</a> in your browser and you will be able to see the text “<em>Hello World</em>” being displayed. This means that the app is running properly.</p><p>Till now we have used http while setting up the server. HTTP is useful while we are directly accessing the uWSGI app but it also adds a TCP-overhead. Using file sockets in this case would be more efficient and avoid the overhead. Moreover, we do not want to access our uWSGI server directly though HTTP anyway.</p><p>Change the line http = 127.0.0.1:8001 with socket = myapp.sock. Run uwsgi myapp.ini again. You will notice you are no longer able to get a response in the browser and your uWSGI server logs show invalid request block size error.</p><p>Let’s make it work with Nginx now. By default brew installs nginx.conf file in/usr/local/etc/nginx/. Navigate to it and create a directory named sites-available and cd into it. Create a file named myapp.conf. This will be the configuration file for our uWSGI web-app. Add the following to it —</p><pre>upstream myapp {<br>    server unix:/Users/Ekwinder/myapp/mysite.sock; # for a file socket<br>    #server 127.0.0.1:8001; # for a web port socket (use this to test with http instead of socket)<br>}</pre><pre>server {<br>    listen 8000;<br>    server_name localhost;</pre><pre>location / {<br>        include uwsgi_params;<br>        uwsgi_pass myapp;<br>        }<br>}</pre><p>Now, change the directory path to your project path in the server section. The uwsgi_params file is included with the Nginx distribution, you just need to include it. Use the uwsgi_pass directive to set the server address for uWSGI or TCP socket depending on what you’re using.</p><p>Create another directory named sites-enabled inside /usr/local/etc/nginx/ and create a symlink for myapp.conf inside it.</p><pre>ln -s /usr/local/etc/nginx/sites-available/myapp.conf myapp.conf</pre><p>Now, let’s make sure Nginx is able to see it. Go back into the /usr/local/etc/nginx directory and open the default nginx config file nginx.config. Add these two lines under the http directive -</p><pre>include /etc/nginx/conf.d/*.conf;<br>include /usr/local/etc/nginx/sites-enabled/*.conf;</pre><p>Reload Nginx using sudo nginx -s reload and if everything goes fine then you should be able to see your uWSGI application running on <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000</a> and “Hello World” message being displayed.</p><p>Congratulations, you are now running your uWSGI web-app with Nginx.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25edf4edab19" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>