nginx serve static files such as .py or other files - nginx

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.

Related

stub_status metrics page used in nginx does not show

I have this .conf file for nginx
server {
listen 8080;
server_name _;
location /status {
stub_status;
}
}
After I used it, I have reloaded NGINX and found out that on my_ip:8080/status there is no page. I checked nginx.conf and it has include /etc/nginx/conf.d/*.conf; where my .conf is located originally.
What could be the problem?
The config looks OK.
What version of nginx you are running? Prior to version 1.7.5 directive stub_status required an argument: stub_status on;
Is your nginx built with corresponding module? To be sure, you can run nginx -V command and check if there in --with-http_stub_status_module in listed config parameters. If not, you need to rebuild nginx with this module enabled.
Is your .conf really loaded by nginx? Try to dump whole congfiguration with nginx -T command and check, if your config is present there.

Wrong mime type text/html with Angular, nginx, Kubernete on Docker Desktop

My setup:
One Docker container with nginx providing an Angular web-app.
One deployment, one service (LoadBalancer) in K8s (Docker Desktop).
nginx.conf
load_module modules/ngx_http_subs_filter_module.so;
http {
include /etc/nginx/mime.types;
server_tokens off;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
}
mime.types contains a huge list of mime types.
When I access the service with my browser I get
where all the mime types are set to text/html. (The browser blocks loading the js and css. "wrong mime type")
I work's without errors locally and on an AKS cluster.
How can I fix it and get the proper mime types?
Update:
I did some more debugging and found out that the mime types are even wrong when I connect to the nginx-container and call the site with curl. Therefore it must be an nginx issue. 🤔
Sorry, my fault!
Wrong configuration because of missing ingress on local (Docker Desktop) setup.
On my AKS system I have an additional ingress which rewrites the URL (removes a part).
The URL of the resources above where like localhost/portal/env.js
These localhost/portal URLS were redirected to index.html.
All the CSS and JS files just contained the index.html and therefore had the proper mime type.
I just needed to add an rewrite to my nginx.conf to fix the issue:
location /portal/ {
rewrite ^/portal/(.*)$ /$1 break;
}

Wordpress & Nginx with Docker: Static files not loaded

I'm using Docker to serve my simple WordPress website. A nginx container and a wordpress container. Simple setup:
upstream wordpress_english {
server wordpress_en:80;
}
server {
listen 80;
server_name my_domain.com www.my_domain.com;
location / {
proxy_pass http://wordpress_english;
}
}
Problem: Static files (css, js and images) are not loaded.
The output from the browser console shows a 404:
http://wordpress_english/wp-content/themes/twentyfifteen/genericons/genericons.css?ver=3.2
Its easy to spot the problem: The browser looks for the static files at wordpress_english (the nginx upstream name), instead of my_domain.com
How can I fix this problem?
This is not a nginx problem, but a WordPress problem.
Solution:
In wp-config.php, add the following two lines:
define('WP_HOME','http://my_domain.com');
define('WP_SITEURL','http://my_domain.com');
During the WordPress installation, WordPress automatically sets WP_HOME to nginx upstream name. The above solution overwrites the default setting.
Seems to be an issue in your nginx config file.
When declaring your server my_domain you provide location / with proxy_pass wordpress_english. I don't know a lot on nginx, but I don't see any declaration of path in your server my_domain and is root is linked to wordpress_english. Seems normal that he is looking for files in wordpress_english and not in you server. (In fact, I guess he is looking in your server but your server tells to look in wordpress).
Not sure about it cause I don't know well nginx and proxy_pass functions.

nginx ignores my site configuration

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.

uwsgi and nginx simple setup without virtual env

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.

Resources