are there any steps to setup uwsgi with nginx with a simple wsgi python script. Most of the places I see only django and flask and other frameworks are being setup. Also I need steps to serve static files.. are there any .. ?
Obviously there are two steps: uwsgi configuration and nginx configuration.
The simplest uwsgi configuration is as follows (uwsgi accepts many different configuration formats, in this example I use xml):
<uwsgi>
<chdir>/path/to/your/script/</chdir>
<pythonpath>/path/to/your/script/</pythonpath>
<processes>2</processes>
<module>myscript.wsgi:WSGIHandler()</module>
<master/>
<socket>/var/run/uwsgi/my_script.sock</socket>
</uwsgi>
The only tricky option here is module, it should point to your WSGI handler class.
Also, make sure that /var/run/uwsgi/my_script.sock is readable and writeable for both uwsgi and nginx.
The corresponding nginx configuration would look like this:
server {
listen 80;
server_name my.hostname;
location / {
include uwsgi_params;
uwsgi_pass unix:/var/run/uwsgi/my_script.sock;
}
}
If you need to serve static files, the smiplest way would be to add following code to the server clause:
location /static/ {
alias /path/to/static/root/;
gzip on;
gzip_types text/css application/x-javascript application/javascript;
expires +1M;
}
This example already includes gzip compression and support for browser cache.
Related
I have site served from S3 with Nginx with following Nginx configuration.
server {
listen 80 default_server;
server_name localhost;
keepalive_timeout 70;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript;
location / {
proxy_pass http://my-bucket.s3-website-us-west-2.amazonaws.com;
expires 30d;
}
At present whenever I build new version, I just delete target bucket contain and upload new frontend files to it.
Since I am deleting bucket contain, there is no way I can go back to previous version of frontend even versioning is enabled on bucket. So want to upload new frontend files into version dir (for example 15) in S3 bucket and then setup a redirect from http://my-bucket.s3-website-us-west-2.amazonaws.com/latest to http://my-bucket.s3-website-us-west-2.amazonaws.com/15
anyone knows how this can be done ?
There are multiple ways to do this:
The easiest may be through a symbolic link, provided that your environment allows that.
ln -fhs ./15 ./latest
Another option is an explicit external redirect issued to the user, where the user would see the new URL; this has a benefit in that multiple versions could be accessed at the same time without any sort of synchronisation issues, for example, if a client decides to do a partial download, everything should still be handy, because they'll most likely be doing the partial download on the actual target, not the /latest shortcut.
location /latest {
rewrite ^/latest(.*) /15$1 redirect;
}
The final option is an internal redirect within nginx; this is usually called URL masquerading in some third-party applications; this may or may not be recommended, depending on requirements; an obvious deficiency would be with partial downloads, where a resume of a big download may result in corrupted files:
location /latest {
rewrite ^/latest(.*) /15$1 last;
}
References:
http://nginx.org/r/location
http://nginx.org/r/rewrite
One of the simple ways to handle this situation is using variables. You can easily import a file to set the current latest version. You will need to reload your nginx config when you update the version with this method.
Create a simple configuration file for setting the latest version
# /path/to/latest.conf
set $latest 15;
Import your latest configuration in the server block, and add a location to proxy to the latest version.
server {
listen 80 default_server;
server_name localhost;
# SET LATEST
import /path/to/latest.conf;
location / {
proxy_pass http://s3host;
expires 30d;
}
# Note the / at the end of the location and the proxy_pass directive
# This will strip the "/latest/" part of the request uri, and pass the
# rest like so: /$version/$remaining_request_uri
location /latest/ {
proxy_pass http://s3host/$latest/;
expires 30d;
}
...
}
Another way to do this dynamically would be to use lua to script this behavior. That is a little more involved though, so I will not get into that for this answer.
I'm using nginx as my server and want to serve some static files, mostly code files, such as .py .java files. but when I do this, nginx directly make the browser download the files as visit
http://localhost:8001/test.py
I know that should be Conten-Type , but I've already configured. below is part of sample nginx config file.
default_type text/plain;
server {
listen 8001;
server_name localhost;
location / {
add_header Content-Type text/plain;
root /path/to/files/;
}
}
so, how to make the browser directly display the file instead of download? Just use nginx static serve or need add some configs?
thx a lot.
ok, I know how to do that.just forgot to do that :(
just add the config
autoindex on;
to server or location section in nginx.
that's waht I want.
I created site config file in dir /etc/nginx/sites-enabled
server {
listen 80;
server_name domain.com;
gzip_types application/x-javascript text/css;
access_log /var/log/nginx/nodeApp.info9000p.access.log;
location / {
proxy_pass http://127.0.0.1:9000/;
}
location ~ ^/(min/|images/|bootstrap/|ckeditor/|img/|javascripts/|apple-touch-icon-ipad.png|apple-touch-icon-ipad3.png|apple-touch-icon-iphone.png|apple-touch-icon-iphone4.png|generated/|js/|css/|stylesheets/|robots.txt|humans.txt|favicon.ico) {
root /root/Dropbox/nodeApps/nodeApp/9000/appdirectory-build;
access_log off;
expires max;
}
}
and restarted nginx:
sudo /etc/init.d/nginx restart
But nginx ignore my site config file and shows default page, when I request domain.com :
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
Where are your Wordpress-files located?
The default nginx htdocs-root is /usr/share/nginx/html
If you've installed the files at, say, /usr/share/nginx/html/wordpress, then you must change the root-setting in the file /etc/nginx/conf.d/default.conf to that directory, like this
server {
root /usr/share/nginx/html/wordpress;
}
Plese read the https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7 article on how to set up nginx to work with the PHP-engine over socket (CentOS) if you have not already done so.
Don't know how, but my configuration started to work well. Maybe because I restarted nginx instead of reload it.
I want to configure my staging environment in Elastic Beanstalk to always disallow all spiders. The nginx directive would look like this:
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
I understand that I would want to create a file under the .ebextensions/ folder, such as 01_nginx.config, but I'm not sure how to structure the YAML inside it such that it would work. My goal is to add this location directive to existing configuration, not have to fully replace any existing configuration files which are in place.
There is an approach which uses the more recent .platform/nginx configuration extension on Amazon Linux 2 (as opposed to older AMIs).
The default nginx.conf includes configuration partials in two locations of the overall nginx.conf file. One is immediately inside the http block, so you can't place additional location blocks here, because that's not syntactically legal. The second is inside the server block, though, and that's what we need.
This second location's partial files are included from a special sub-directory, .platform/nginx/conf.d/elasticbeanstalk. Place your location fragment here to add location blocks, like so:
# .platform/nginx/conf.d/elasticbeanstalk/packs.conf
location /packs {
alias /var/app/current/public/packs;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
I wanted to do the same thing. After a lot of digging, I found 2 ways to do it:
Option 1. Use an ebextension to replace the nginx configuration file with your custom configuration
I used this option because it is the simplest one.
Following the example given by Amazon in Using the AWS Elastic Beanstalk Node.js Platform - Configuring the Proxy Server - Example .ebextensions/proxy.config, we can see that they create an ebextension that creates a file named /etc/nginx/conf.d/proxy.conf. This file contains the same content as the original nginx configuration file. Then, they delete the original nginx configuration file using container_commands.
You need to replace the Amazon example with the contents of your current nginx configuration file. Note that the nginx configuration files to be deleted in the containter command must be updated too. The ones I used are:
nginx configuration file 1: /opt/elasticbeanstalk/support/conf/webapp_healthd.conf
nginx configuration file 2: /etc/nginx/conf.d/webapp_healthd.conf
Therefore, the final ebextension that worked for me is as follows:
/.ebextensions/nginx_custom.config
# Remove the default nginx configuration generated by elastic beanstalk and
# add a custom configuration to include the custom location in the server block.
# Note that the entire nginx configuration was taken from the generated /etc/nginx/conf.d/webapp_healthd.conf file
# and then, we just added the extra location we needed.
files:
/etc/nginx/conf.d/proxy_custom.conf:
mode: "000644"
owner: root
group: root
content: |
upstream my_app {
server unix:///var/run/puma/my_app.sock;
}
log_format healthd '$msec"$uri"'
'$status"$request_time"$upstream_response_time"'
'$http_x_forwarded_for';
server {
listen 80;
server_name _ localhost; # need to listen to localhost for worker tier
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/access.log main;
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
location / {
proxy_pass http://my_app; # match the name of upstream directive which is defined above
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /assets {
alias /var/app/current/public/assets;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
location /public {
alias /var/app/current/public;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
}
container_commands:
# Remove the default nginx configuration generated by elastic beanstalk
removeconfig:
command: "rm -f /opt/elasticbeanstalk/support/conf/webapp_healthd.conf /etc/nginx/conf.d/webapp_healthd.conf"
Once you deploy this change, you have to reload the nginx server. You can connect to your server using eb ssh your-environment-name and then run sudo service nginx reload
Option 2. Use an ebextension to modify the nginx configuration file generator, so that it includes your custom locations in the final nginx configuration file
The second option is based on this post: jabbermarky's answer in Amazon forums
He explains this method very well in his answer, so I encourage you to read it if you want to implement it. If you are going to implement this answer, you need to update the location of the nginx file configuration generator.
Note that I have not tested this option.
In summary, he adds a shell script to be executed before the nginx configuration file is generated. In this shell script, he modifies the nginx configuration file generator to include the server block locations he wants in the generated nginx configuration file. Finally, he adds a file containing the locations he wants in the server block of the final nginx configuration file.
It seems that the mentioned approaches dont work anymore. The new approach is to place nginx .conf files into a subfolder in .ebextensions:
You can now place an nginx.conf file in the .ebextensions/nginx folder to override the Nginx configuration. You can also place configuration files in the .ebextensions/nginx/conf.d folder in order to have them included in the Nginx configuration provided by the platform.
Source
This does not require a restart of nginx either as Elastic Beanstalk will take care of that.
Mmmmm! .ebextensions!
You're probably easiest off creating a shell script to change your configuration, and then running that. Don't really know nginx, but try something along the lines of:
files:
"/root/setup_nginx.sh" :
mode: "000750"
owner: root
group: root
content: |
#!/bin/sh
# Configure for NGINX
grep robots.txt <your_config_file> > /dev/null 2>&1
if [ $? -eq 1 ] ; then
echo < EOF >> <your_config_file>
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
EOF
# Restart any services you need restarting
fi
container_commands:
000-setup-nginx:
command: /root/setup_nginx.sh
I.e. first create a schell script that does what you need, then run it.
Oh, and be careful there are no tabs in your YAML! Only spaces are allowed... Check the log file /var/log/cfn_init.log for errors...
Good luck!
For version Amazon Linux 2 use this path on your bundle and zip this foldes together
.platform/nginx/conf.d/elasticbeanstalk/000_my_custom.conf
This is what's working for me:
files:
"/etc/nginx/conf.d/01_syncserver.conf":
mode: "000755"
owner: root
group: root
content: |
# 10/7/17; See https://github.com/crspybits/SyncServerII/issues/35
client_max_body_size 100M;
# SyncServer uses some http request headers with underscores
underscores_in_headers on;
# 5/20/21; Trying to get the load balancer to respond with a 503
server {
listen 80;
server_name _ localhost; # need to listen to localhost for worker tier
location / {
return 503;
}
}
container_commands:
01_reload_nginx:
command: pgrep nginx && service nginx reload || true
To fix this - you need to wrap your configuration file. You should have, if you're using Docker, a zip file (mine is called deploy.zip) that contains your Dockerrun.aws.json. If you don't - it's rather easy to modify, just zip your deploy via
zip -r deploy.zip Dockerrun.aws.json
With that - you now need to add a .platform folder as follows:
├── .platform
│ └── nginx
│ └── conf.d
│ └── custom.conf
You can name your custom.conf whatever you want, and can have as many files as you want. Inside custom.conf, you simply need to place the following inside
client_max_body_size 50M;
Or whatever you want for your config. With that - modify your zip to now be
zip -r deploy.zip Dockerrun.aws.json
And deploy. Your Nginx server will now respect the new command
This is achievable using .ebextension config files, however I'm having difficulty kicking nginx to restart after a change to its configuration files.
# .ebextensions/nginx.config
files:
"/etc/nginx/conf.d/robots.conf":
mode: "000544"
owner: root
group: root
content: |
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
encoding: plain
Now, I've done similar to add a file to kick the nginx tyres, however for some odd reason it's not executing:
"/opt/elasticbeanstalk/hooks/appdeploy/enact/03_restart_nginx.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/containerfiles/envvars
sudo service nginx restart
ps aux | grep nginx > /home/ec2-user/nginx.times.log
true
encoding: plain
I got a new slice off slicehost, for the purposes of playing around and learning nginx and more about deployment generally. I installed a ruby app on there (which i'll call app1) which uses passenger. I made it the default app to use for that server with the following server block in my nginx config:
server {
listen 80;
server_name <my server ip>;
root <path to app1 public folder>;
passenger_enabled on;
}
This works fine. However, i want to try a few different apps out on this slice, and so thought i would set it up like so:
http:///app1
http:///app2
etc. I thought i would be able to do that by adding a location block, and moving the app1 specific stuff into it like so:
server {
listen 80;
server_name <my server ip>;
location ^~ /app1 {
root <path to app1 public folder>;
passenger_enabled on;
}
}
However, on doing this (and restarting nginx of course), going to the plain ip address gives the 'welcome to nginx' message (which i'd expect). But, going to /app1 gives an error message:
404 Not Found
The requested URL /app1 was not found on this server.
This is distinct from the error message i get when i go to another path on that ip, eg /foo:
404 Not Found
nginx/0.8.53
So, it's like nginx knows about that location but i've not set it up properly. Can anyone set me straight? Should i set up different server blocks instead of using locations? I'm sure this is simple but can't work it out.
Cheers, max
What you're after is name virtual hosting. The idea is that each domain is hosted on the same IP, and nginx chooses the virtualhost to serve based on the Host: header in the HTTP request, which is sent by the browser.
To use name virtual hosting, use the domain you want to serve instead of your server's IP for the server_name directive.
server {
listen 80;
server_name app1.com;
location / {
root /srv/http/app1/public;
passenger_enabled on;
}
}
Then, to host more apps on the same box, just declare a separate server { } block for each one.
server {
listen 80;
server_name app2.com;
location / {
root /srv/http/app2/public;
passenger_enabled on;
}
}
I'm using unicorn instead of passenger, but the vhost part of the structure is the same for any backend.
The global nginx config (which on its own hosts nothing): https://github.com/benhoskings/babushka-deps/blob/master/nginx/nginx.conf.erb
The template wrapper for each virtualhost: https://github.com/benhoskings/babushka-deps/blob/master/nginx/vhost.conf.erb
The details of the unicorn virtualhost: https://github.com/benhoskings/babushka-deps/blob/master/nginx/unicorn_vhost.common.erb
I fail to see the real problem here tho,
in order for you to figure that out
you need to view the nginx log files on most systems at:
/var/log/nginx/
and open the relevant access file here(might be error.log)
in there you can see what url nginx exactly tried to access and why did it fail.
What I really think is happening, that you got the root path wrong,
maybe it should be alias instead because
if you are proxifying the connection to another app, it might get the
"app1" word in the url instead of a direct one.
so please try:
server {
listen 80;
server_name <my server ip>;
location /app1 {
alias <path to app1 public folder>;
passenger_enabled on;
}
}
and see weather it works and also try to view the logs first to really determine whats the problem.
I think its just a slight syntax problem:
location ~ ^/app1 { ...
should work, or a little more efficient:
location = /app1 { ...
One problem is that your Rails app probably wasn't designed to run from a subdirectory. Passenger has a directive that will fix this:
passenger_base_uri /app1;
However, running Rails apps in subdirectories is somewhat non-standard. If you can, a better option may be to set up subdomains using nginx's virtual hosts.
It seems that you want to host more apps on the same server with base uri. Try this:
root /srv/http/;
passenger_base_uri /app_1;
passenger_base_uri /app_2
Also under /srv/http, create 2 symlinks:
ln -s /srv/http/app_1 /srv/http/app1/public
ln -s /srv/http/app_2 /srv/http/app2/public
The app1 can be accessed under: http://domain.com/app_1.
Here is more for reading: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rack_to_sub_uri