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.
Related
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;
}
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>').
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.
I have an Ubuntu VPC. I have a nodejs app running on it in some folder. I have a php app running on apache in var/www. What I want is to configure nginx so that when a user comes on mydomain.com , he/she is redirected to my node app running on port 3000 and when the user visits mydomain.com/docs , he/she is redirected to my php app running on apache on port 80.
server {
listen 80;
root /var/www;
location / {
try_files $uri $uri/ /index.html;
}
location ~\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~/\.ht {
deny all;
}
}
My conf file for nodejs app is:
upstream app_somedomain {
server 127.0.0.1:3000;
}
server {
listen 0.0.0.0:80;
server_name mydomain.com mydomain;
root /some/path;
index index.html index.htm index.php index.js;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_somedomain/;
proxy_redirect off;
}
}
I am using nginx for the first time. I am still unable to understand how to achieve this. As in how to serve my nodejs app running on port 3000 to my domain name and mydomain/docs to my php app.
Thanks.
I am really confused whether it is possible ? Please help me out, I have Node.js application, say node_app, running on X port, and PHP application, say my_app, running in Apache's default 80 port. I have one domain name only. What my problem is, if user hit domain.com/my_app, it should run the PHP application in 80's port. If the user hits domain.com/node_app, it should run the node application in X port. And one more important constraint is the end-user should not see any port number in URL bar.
You can install Node.JS and PHP in the same host, using Nginx as proxy per exemple.
Per exemple, with Nginx, you could create two virtualhosts :
Default virtual host using PHP (FPM or not) who points to exemple.tld
Second virtual host to another node.exemple.tld
First VH is gonna be like this (with PHP-FPM) :
server {
listen 80; ## listen ipv4 port 80
root /www;
index index.php index.html index.htm;
# Make site accessible from exemple.tld
server_name exemple.tld;
location / {
try_files $uri $uri/ /index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 and using HHVM or PHP
#
location ~ \.(hh|php)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
Second VH with NodeJS :
server {
listen 80;
server_name node.exemple.tld;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
access_log off;
# Assuming that NodeJS listen to port 8888, change if it is listening to another port!
proxy_pass http://127.0.0.1:8888/;
proxy_redirect off;
# Socket.IO Support if needed uncomment
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
}
# IF YOU NEED TO PROXY A SOCKET ON A SPECIFIC DIRECTORY
location /socket/ {
# Assuming that the socket is listening the port 9090
proxy_pass http://127.0.0.1:9090;
}
}
As you can see, it's possible, and pretty easy to do!