Adding Flask app in existing nginx PHP configuration - php

The configuration below is the configuration for a web application in PHP, and it's working (I faked the site's name to https://sub.mysite.nl).
server {
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
## some certificate info ##
root /path/to/www;
index index.php index.htm index.html;
server_name sub.mysite.nl;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
## some logging info ##
}
server {
if ($host = sub.mysite.nl) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
server_name sub.mysite.nl;
return 404; # managed by Certbot
}
Now I want to add a Flask app in a subfolder e.g. https://sub.mysite.nl/flaskapp.
The block below is what I got from the Flask Mega Tutorial I followed, see specifically this chapter: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux (under Setting Up Nginx). I think I need to put this under location /flaskapp/ but I'm not sure how to proceed, because when I do this and go to https://sub.mysite.com/flaskapp it gives me 404 Not Found.
location /flaskapp {
proxy_pass http://localhost:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Do I need to change the routing in my Flask app?

Ok, I've been fooling around a bit and it seems like editing the routes in my Flask app provides the easiest solution. For this, I use the Flask nginx block from my original post at the bottom.
So instead of #app.route('/'), I use #app.route('/flaskapp/').
And #app.route('/view_profile/<username>') becomes #app.route('/flaskapp/view_profile/<username>').

Related

php is being downloaded instead of being loaded

I got a nginx docker container which actually works. Because when i do curl localhost:7070 on my host i get the content of the site as return. Then i made a config for nginx on my host, the problem is when i try to open the site in a browser it downloads the php file. I will sahre both configs of host nginx and docker nginx.
Nginx host conf:
server {
#listen 80 default_server;
#listen [::]:80 default_server;
# SSL configuration
listen 7171 ssl default_server;
listen [::]:7171 ssl default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name SERVERNAME;
location / {
proxy_pass http://127.0.0.1:7070;
#proxy_redirect off;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Host $server_name;
}
}
nginx docker conf:
server {
listen 80;
server_name default_server;
root /usr/share/nginx/selfoss;
index index.php index.html;
location ~* \ (gif|jpg|png) {
expires 30d;
}
location ~ ^/(favicons|thumbnails)/.*$ {
try_files $uri /data/$uri;
}
location ~* ^/(data\/logs|data\/sqlite|config\.ini|\.ht) {
deny all;
}
location / {
index index.php;
try_files $uri /public/$uri /index.php$is_args$args;
}
}
I know alittle about nginx normal config but im noob at php so what is the thing that im missing here?
I have seen posts about php-fpm and fastcgi but i have no clue ho or where to do that config changes.
You are missing a configured PHP-FPM in your Nginx-Config and of course a running PHP-FPM (inside the same Docker-Container or in a separate Docker-Container).
Example Nginx-Config inside your server{} section
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
proxy_read_timeout 300;
fastcgi_read_timeout 300;
fastcgi_pass unix:/var/run/sockets/php/www.sock;
}

Facing redirection issue while setting up Wordpress with Nginx via Amazon API Gateway and Network Load Balancer

Facing redirection issue while setting up Wordpress with Nginx via Amazon API Gateway and Network Load Balancer.
Description:-
We have our main website xyz.com(served by Amazon API Gateway and Load Balancer) and want our blogs to be present on xyz.com/blogs.
So, we have set up Amazon API Gateway and Load Balancer to redirect any request of the for xyz.com/blogs to the EC2 containing Wordpress with Nginx.
Problem:-
The problem that we are facing is, the home page is rendered fine but when we try to render any other page, e.g:- xyz.com/blogs/my-first-post/ or xyz.com/blogs/wp-admin then it gets stuck over there and nothing comes as response. As a part of our initial debugging, we found out that Wordpress is making redirections to the Network Load Balancer url, (which as per our guess) is not accessible and we are not getting any response.
This is how our default nginx conf looks like (/etc/nginx/conf.d/xyz_blogs.conf), which we got from this link => Wordpress|Nginx
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
## Your website name goes here.
server_name xyz.com;
## Your only path reference.
root /var/www/html;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
How shall we resolve this issue?
A prior thanks for any help given hereby.
Thy this:
upstream php-app { # <-- change name of the upstram
# server unix:/tmp/php-cgi.socket; # <-- Remove this
# List of [IP:Port] OR list of [sockets] not both mixed
server 127.0.0.1:9000;
# server 127.0.0.1:9001; # example
# server 127.0.0.1:9002; # example
}
server {
listen 80 default_server; # <-- Add this line
listen [::]:80 default_server; # <-- Add this line
server_name xyz.com *.xyz.com;
root /var/www/html;
# index index.php; # <-- Will be removed, you app is provided by the socket/port
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location / { # Put this at the end, NGinx apply priority
proxy_pass http://php-app; # Add this line
}
}
Update 1:
update config
...
server_name xyz.com *.xyz.com *.amazonaws.com;
...
Sorry for the late post, but currently we have implemented this using a solution which might not be much scalable. What we have done is we have deployed our website and wordpress in the same machine but in a different containers. So, our website is running in a different container and wordpress is running in a different container but both are present in the same EC2.
And we are using nginx configuration in wordpress to redirect the requests to the fellow container. Currently my nginx configuration looks something like this:
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
listen 80;
server_name xyz.com;
root /var/www/html;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
index index.php;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
location / {
rewrite ^/(.*) /$1 break;
proxy_pass http://xyz-container:80;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
It would have been great if somehow we would have been able to deploy wordpress container in a different EC2 and still achieve this.
It will really be helpful and a lot better if someone can suggest a better scalable working solution for the problem with wordpress container in a different EC2.
Thanks.

NGINX Index 404 Not Found

I've just installed a Ghost Blog on a new server running NGINX. The Ghost config.json file is pointing at the correct directory /blog and the blog loads fine when I visit it.
What isn't working is when I remove /blog from the URL, I get taken to a 404 page. I've checked my sites-enabled file, which looks like this:
server {
listen 80;
listen [::]:80;
server_name *********;
root /var/www/ghost/system/nginx-root;
location ^~ /blog {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://*********:2368;
proxy_redirect off;
}
location ~ /.well-known {
allow all;
}
client_max_body_size 50m;
But I'm not entirely sure what I need to change to not get the 404 error. I have an example .php file which should be loading but isn't.
I've always used the Digital Ocean One-Click Ghost app but I wanted to use the Ghost CLI this time round. I have a feeling I've missed something though.
the following may remove some of your restrictions but it will work
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl on;
ssl_certificate /etc/letsencrypt/live/thedomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/thedomain.com/privkey.pem;
access_log /var/log/nginx/thedomain.access.log;
error_log /var/log/nginx/thedomain.error.log;
root /var/www/thedomain;
index index.html;
gzip on;
gzip_proxied any;
gzip_types text/css text/javascript text/xml text/plain application/javascript application/x-javascript application/json;
location / {
try_files $uri $uri/ =404;
}
}
You need to make sure all the ssl files are there and permissioned for access by www-data.
If you need to run certbot for the first time, just but the 443 code in an 80 block without the ssl statements
The nginx configuration you've posted only deals with Ghost.
You've setup a server responding on port 80, set the root to Ghost's nginx-root, and created 2 location blocks. One is for /blog/ and serves Ghost, the second .well-known block is for handling generation of SSL certificates with letsencrypt.
I'm not an expert at configuring nginx for PHP, but this guide from Digital Ocean and this stackoverflow question covers a lot of the details.
I think you have a couple of options:
Set the index to be index.php
Add a new location block for / which serves php files
Add a block to handle all php files
I believe adding a new location block like this, will mean any .php files you have will always be called if the path in the URL matches.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
But the value of fastcgi_pass will depend on how you have PHP setup on your system.
With this, going to /index.php should work.
Setting index: index.php will mean that / maps to /index.php I'm not sure if that will interfere with Ghost, if it does, you'd need a specific location / {} block instead of the index being set.

Nginx + php5-fpm = 404 Error with alias location

I am an amateur front end web developer, and I recently bought a Ubuntu server to try to my hand at some backend development. I am trying to figure out how to serve a php file from an aliased location block using php5-fpm. I am getting a 404 - Page not found error. I have tried all of the proposed solutions I could find here with no luck. As I am still a beginner I would love a quick ELI5 as well and any pointers on the rest of my conf file, so I can learn something too. I should mention that the main root folder is running a flask app, and is the reason I am using an aliased location.
My virtual host:
Nginx conf file
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;
root /var/www/example;
large_client_header_buffers 8 32k;
access_log /var/www/example/logs/access.log;
error_log /var/www/example/logs/error.log;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr; #$proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_test;
proxy_redirect off;
}
location /test_site {
alias /var/www/test_site;
index index.php index.html index.htm;
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
php5 www.conf file
[www]
...
user = www-data
group = www-data
listen = 127.0.0.1:9000
#listen = /tmp/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
...
My fastcgi_params file is default. I have checked both the php and nginx logs and there are no errors. Any help would be much appreciated!
Getting alias to work with nested locations using fastcgi is complicated.
Assuming that you have not over simplified your configuration, the test_site location does not need to use alias:
location /test_site {
root /var/www;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:127.0.0.1:9000;
include fastcgi_params;
}
}
This removes the alias directive, and solves the aliasing problem in the PHP block.
Note also: The regex on the location ~ \.php$ block was wrong. The fastcgi_split_path_info and fastcgi_index directives are unnecessary.
The nginx directives are documented here.

Nginx multiple listeners (with php application)

I want Nginx to serve both my jenkins server and my Laravel (php) application. Here is my Nginx configuration:
server {
listen 80 default_server;
server_name localhost;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Fix the "It appears that your reverse proxy set up is broken" error.
proxy_pass http://127.0.0.1:8081;
proxy_read_timeout 90;
proxy_redirect http://127.0.0.1:8081 https://jenkins.domain.tld;
}
}
server {
listen 9000;
listen [::]:9000;
root /var/www/my-app/public;
index index.php index.html index.htm;
server_name my-app.io;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am using virtual-box as my virtual machine emulator for ubuntu 14. I am able to access jenkins application by going to http://127.0.0.1:8081 (I have changed the jenkins default port).
But when I am trying to access http://127.0.0.1:9000 I am getting an error, I have checked Ngnix logs but couldn't find any information regarding a request to port 9000.
Also I have made port forwarding in virtual-box, here is a screenshot:
Any help would be great.
Use server_name localhost too for php.

Categories