How to configure nginx to run Wordpress and Django - django

I've looked at a couple similar question, but they didn't seem to be any help. I've got a vultr.com instance running a Wordpress in the default 1 click configuration. Centos6 if that matters. This is (and I would like it to remain) in the root (www.mysite.com). I would like to have my Django app running in www.mysite.com/crossfaded/. I don't want any downtime for my current server, so there is no domain name associated with the server yet. I'm trying to do this just using the IP.
The Wordpress site is working fine.
I've been following the guide here as then tried this one, but when I navigate to http://ip.add.re.ss/crossfaded/media/apple.jpg in my browser, I get a 404 from nginx. Going /crossfaded/media/ gives me a 403 from nginx and /crossfaded/invalidpath/ gives me a 404 served by Wordpress, so something is happening with the routing. I did chmod 777 apple.jpg on the off-chance it was a permissions issue, but that didn't do anything.
I have a hunch I've got the syntax of the location block muddled, but I'm really not sure.
wordpress_http.conf
upstream php-handler-http {
server 127.0.0.1:9000;
#server unix:/var/run/php5-fpm.sock;
}
server {
listen 80 default_server;
server_name _;
#server_name wordpress.example.com;
root /var/www/html/;
index index.php;
# set max upload size
client_max_body_size 2G;
fastcgi_buffers 64 4K;
access_log /var/log/nginx/wordpress_http_access.log combined;
error_log /var/log/nginx/wordpress_http_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args ;
}
location ^~ /wp-admin/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd/wpadmin;
location ~* \.(htaccess|htpasswd) {
deny all;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-handler-http;
fastcgi_read_timeout 60s;
}
}
location ~* \.(htaccess|htpasswd) {
deny all;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-handler-http;
fastcgi_read_timeout 60s;
}
# set long EXPIRES header on static assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
access_log off;
}
location ~ /crossfaded/static/ {
alias /root/crossfaded/static/;
}
location ~ /crossfaded/media/ {
alias /root/crossfaded/media/ ;
}
}

There were two problems:
I needed a caret (^) in the location of the alias, so:
location ^~ /crossfaded/static/ {
alias /root/crossfaded/static/;
}
location ^~ /crossfaded/media/ {
alias /root/crossfaded/media/ ;
}
The server defaulted ~/ to /root/. Nginx needs r-x permissions to all directories from / to the file in question. It did not have that. Moved the project directory to /home/.

Related

NGINX ERROR :connect() failed (111: Connection refused) while connecting to upstream

I get that error in title when I cat out the error.log
this is how I set my website config inside /etc/nginx/site-availables/ArticleWebsite:
server_tokens off;
access_log /var/log/nginx/ArticleWebsite.access.log;
error_log /var/log/nginx/ArticleWebsite.error.log;
# This configuration will be changed to redirect to HTTPS later
server {
server_name backend.globeofarticles.com;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/backend.globeofarticles.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/backend.globeofarticles.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
}
}
to explain my situation better, backend.globeofarticles.com is the subdomain, that where the requests are sent from globeofarticles.com or www.globeofarticles.com.
Also, Django has 127.0.0.1:8000 host as default.
when I access the website (backend subdomain) I get this error:
when checking network tab, I get too many redirects actually:
with status code 301
try this for ur nginx config. then u can reinstall certbot for this domain. using certbot --nginx
server {
server_name backend.globeofarticles.com;
root /var/www/backend.globeofarticles.com/html/;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# A long browser cache lifetime can speed up repeat visits to your page
location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
# disable access to hidden files
location ~ /\.ht {
access_log off;
log_not_found off;
deny all;
}

How to serve images from a different root when a certain page of the website. Nginx

I don't have much experience with regex, and less in an Nginx context, so this one is a difficult one for me.
What I want is that when I'm on a especific page (not subfolder) the server takes images from a different root.
My current configuration to handle images in all the pages of my website right now is this one.
location ~* \.(png|jpg|jpeg|gif) {
root /usr/share/nginx/nginxTestPHP/media;
}
And I'd like that when I'm in the page example.com/My-Profile the server handles the images from /usr/share/nginx/nginxTestPHP/media/uploads, my idea is something like this, if it makes any sense.
location ~* /My-Profile/*\.(png|jpg|jpeg|gif) {
root /usr/share/nginx/nginxTestPHP/media/uploads;
}
Obviously this one doesn't work otherwise I wouldn't be here asking this, so for the regex pros out there, what would be the solution for this one ?
Also how would I apply this in 2 different pages in the same regex, something like (My-Profile|Configuration) is my idea.
My Nginx configuration
server {
listen 81;
listen [::]:81;
server_name IP;
client_max_body_size 4M;
charset UTF-8;
include /etc/nginx/mime.types;
error_log /var/log/nginx/error_log warn;
access_log /var/log/nginx/access_log main;
rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
rewrite ^/(.*)/$ /$1 permanent;
root /usr/share/nginx/nginxTestPHP/PHP;
index index.html index.htm Inicio.php Index.php;
location / {
try_files $uri/index.html $uri.html $uri/ #extensionless-php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# pass the PHP scripts to FastCGI
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_pass app:9000;
fastcgi_index Inicio.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
location ^~ /vendor/ {
deny all;
return 403;
}
# deny access to .htaccess files
location ~ /\.ht {
deny all;
}
location ^~ /en/ {
try_files $uri/.php $uri.php $uri/ #rewrite;
fastcgi_intercept_errors on;
fastcgi_pass app:9000;
fastcgi_index Index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
location ^~ /Blog/ {
try_files $uri/.php $uri.php $uri/ #rewrite;
fastcgi_intercept_errors on;
fastcgi_pass app:9000;
fastcgi_index Index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
location #rewrite {
rewrite ^/(en|Blog)(/.*\.(png|jpg|svg|ico))$ $2 last;
return 404;
}
location ~ \.css {
root /usr/share/nginx/nginxTestPHP/CSS;
default_type text/css;
add_header Content-Type text/css;
}
location ~ \.js {
root /usr/share/nginx/nginxTestPHP/JavaScript;
default_type text/javascript;
add_header Content-Type application/x-javascript;
}
location ~ \.txt {
root /usr/share/nginx/nginxTestPHP/PHP;
}
location ~* ^/Mi-Perfil/([^/]+\.(png|jpg|jpeg|gif))$ {
alias /usr/share/nginx/nginxTestPHP/media/uploads/$1;
}
location ~* \.(png|jpg|svg|ico|jpeg|gif) {
root /usr/share/nginx/nginxTestPHP/media;
}
error_page 405 =200 $uri;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/nginxTestPHP/PHP;
try_files $uri /Inicio.php;
}
}
Your regex is broken, it is a dot (.) in PCRE patterns that acts as a wildcard, not an asterisk one. If you want to match any non-zero number of characters except a / one, you can use [^/]+ regex pattern:
location ~* /My-Profile/[^/]+\.(png|jpg|jpeg|gif)$ { ... }
or to ensure that an URI started exactly with /My-Profile/ add a ^ anchor (matches start of the string):
location ~* ^/My-Profile/[^/]+\.(png|jpg|jpeg|gif)$ { ... }
Check the difference between root and alias nginx directives. With your configuration nginx will search the files inside the /usr/share/nginx/nginxTestPHP/media/uploads/My-Profile folder. Use an alias directive instead:
location ~* ^/My-Profile/[^/]+\.(png|jpg|jpeg|gif)$ {
alias /usr/share/nginx/nginxTestPHP/media/uploads;
}
Update
I think I made a serious mistake. Since this is a regex matching location an alias directive argument should contain full path to image file with filename:
location ~* ^/My-Profile/([^/]+\.(png|jpg|jpeg|gif))$ {
alias /usr/share/nginx/nginxTestPHP/media/uploads/$1;
}
Little experience with Nginx here but the regex part looks relatively simple to fix - try using:
location ~* /My-Profile/.+\.(png|jpg|jpeg|gif)$ {
root /usr/share/nginx/nginxTestPHP/media/uploads;
}
Note the .+ after My-Profile/ and the end-of-string anchor ($) - I'm assuming you want to only match images, not a URI like stackoverflow.com/test.png/somepage.
From the little research I did, you may also want to look into nginx maps, especially if you're looking to do this for multiple profile pages all following the same URI pattern.

How to run django and wordpress with nginx

I'm having problem with configuring my nginx.conf file to run django server on main domain and a WordPress site on domain.com/blog.
This is my configuration file which my WordPress dir is
/var/www/varzesh-kon/blog/:
upstream Main_Project_server {
server unix:/home/amirfarsad/django_env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 2n9l.s.serverhost.name;
client_max_body_size 4G;
access_log /home/amirfarsad/logs/nginx-access.log;
error_log /home/amirfarsad/logs/nginx-error.log;
location /static/ {
alias /home/amirfarsad/Main_Project/static/;
}
location /media/ {
alias /home/amirfarsad/Main_Project/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://Main_Project_server;
break;
}
}
location /blog/ {
root /var/www/varzesh-kon/blog/;
index index.php index.html index.htm;
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
root /var/www/varzesh-kon/blog/;
}
}
My django site works well but when I go to domain.com/blog, it gives me a 404 not found nginx page.
Try changing your bottom location block
location ^~ /blog {
root /var/www/varzesh-kon/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
Explanation:
In case of the root directive, full path is appended to the root including the location part
In your case:
location /blog/ {
root /var/www/varzesh-kon/blog/;
The final path that nginx will derive is going to be:
/var/www/varzesh-kon/blog/blog
That's why its showing you 404 not found page
Solution:
Either use alias instead of root
or change root path to /var/www/varzesh-kon/
location /blog/ {
root /var/www/varzesh-kon/;
...
}
location /blog/ {
alias /var/www/varzesh-kon/blog/;
...
}
for more: wiki

phpmyadmin not working on nginx web server in linux ami on amazon ec2 instance

What I am working with :
Instance - amazon EC2,
OS - linux AMI,
Web-server - nginx
I have installed phpmyadmin according to what is mentioned in the AWS documentation here
I have mysql up and running, as well as php-fpm and nginx. I also created a symlink between the directory where phpMyAdmin i.e /var/www/html/phpMyAdmin -> /usr/share/nginx/www/html
below is what I have in my nginx.conf file
server{
listen 80;
server_name localhost;
root /var/www/html/phpMyAdmin;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#location ^~ /phpMyAdmin/ {
# root /var/www/html/phpMyAdmin;
# index index.php;
# include fastcgi_params;
# fastcgi_pass unix:/var/run/php-fpm.sock;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME script/$fastcgi_script_name;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/html/phpMyAdmin;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
But none of this seems to work. I am trying from hours but unable to figure out what may be the problem due to which its not working. any help would be really great. Thanks !
You can try with below vhost config file. Error page redirection handling appears a problem with your config file.
server {
listen 80;
root /var/www/html/phpMyAdmin; # Change the PHPMyAdmin location
index index.php;
server_name default_server;# Set server name based on sub-domain
access_log /var/log/nginx/phpMyAdmin-access.log;
error_log /var/log/nginx/phpMyAdmin-error.log notice;
charset utf-8;
error_page 404 403 500 502 503 504 /index.php;
location / {
try_files $uri $uri/ /index.php?$args;
access_log off;
expires max;
}
location ~* .(jpg|jpeg|png|gif|ico|css|js|ico|swf)$ { expires 365d; access_log off; log_not_found off;}
location = /favicon.ico { log_not_found off; access_log off; allow all; }
location = /robots.txt { access_log off; log_not_found off; allow all; }
location ~ \.php$ {
expires off;
fastcgi_read_timeout 600;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/phpmyadmin.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

Nginx doesn't serve static

I'm running Django on Ubuntu Server 9.04.
Django works well, but nginx doesn't return static files - always 404.
Here's the config:
server {
listen 80;
server_name localhost;
#site_media - folder in uri for static files
location /static {
root /home/user/www/oil/oil_database/static_files;
autoindex on;
}
#location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
# root /home/user/www/oil/oil_database/static_files;
# access_log off;
# expires 30d;
#}
location / {
root html;
index index.html index.htm;
# host and port to fastcgi server
#fastcgi_pass 127.0.0.1:8080;
fastcgi_pass unix:/home/user/www/oil/oil_database/oil.sock;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
access_log /var/log/nginx/localhost.access_log;
error_log /var/log/nginx/localhost.error_log;
}
Nginx version is 0.6.35.
All directories exist and made 777 (debugging paranoia). The commented-out block doesn't help when I uncomment it.
How is your directory setup? Do you have a folder static in /home/user/www/oil/oil_database/static_files? In that case, the directive should look like this (note the trailing slash in /static/):
location /static/ {
autoindex on;
root /home/user/www/oil/oil_database/static_files;
}
If you want to map the path /home/user/www/oil/oil_database/static_files to the URL /static/, you have to either
rename the folder static_files to static and use this directive:
location /static/ {
autoindex on;
root /home/user/www/oil/oil_database/;
}
use an alias:
location /static/ {
autoindex on;
alias /home/user/www/oil/oil_database/static_files/;
}
See the documentation on the root and alias directives.
I have a similar config for my Django sites, but I think you want to use alias instead of root for your media. For example:
location /static {
alias /home/user/www/oil/oil_database/static_files;
}