Nginx multiple listeners (with php application) - php

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.

Related

502 bad gateway when access nginx from a docker container

I am trying to create a php inside my docker container
However, when I go to the localhost on my browser, I keep getting the 502 bad gateway.
The error log shows
[error] 11#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: (my IP), server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
My nginx conf looks like this
server {
listen 80;
server_name jenkins.local;
root /var/www/html;
index index.php;
access_log /var/log/nginx/localhost-access.log;
error_log /var/log/nginx/localhost-error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
I am new to docker and PHP so please let me know if I need to provide any other information.
Did you start php-fpm?
Is port 9000 open?
Brief edition
upstream websocketserver {
server localhost:8080;
}
server {
listen 80;
server_name jenkins.local; //Or try localhost;
root /var/www/html;
index index.php;
access_log /var/log/nginx/localhost-access.log;
error_log /var/log/nginx/localhost-error.log;
location / {
proxy_pass http://websocketserver;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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-Proto https;
proxy_read_timeout 86400; # neccessary to avoid websocket timeout disconnect
proxy_send_timeout 900s;
proxy_redirect off;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock; //Check if your php is 7.4, run php -v
}
}

Nginx proxy PHP-FPM + Lighttpd 405 not allowed symfony

Hello I am trying to serve a php web application using Nginx PHP-FPM using a tcp socket on a remote host and a lighttd to serve static content on a remote server.
I succeed to link those three blocks but I have an issue to manage the interactions.
Here my Nginx proxy configuration
upstream xxxx-staging {
server xxxxx.com:81 fail_timeout=0;
}
server {
listen 80;
server_name xxxxxxx.com;
return 301 https://xxxxx.com$request_uri;
}
server {
listen 443 ssl;
server_name xxxxxx.com;
root /xxxxxx/public;
ssl_certificate /etc/nginx/ssl/xxx.com.pem;
ssl_certificate_key /etc/nginx/ssl/xxx.com.key;
access_log /var/log/nginx/xxx.access.log;
error_log /var/log/nginx/xxx.error.log error;
client_max_body_size 256m;
proxy_intercept_errors on;
error_page 404 = /index.php;
error_page 405 = 200$uri;
location = / {index index.php;}
location / {
proxy_pass http://xxxxx-staging;
proxy_set_header Host $http_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-Proto $scheme;
auth_basic "Please authenticate";
auth_basic_user_file /etc/nginx/passwords/xxxxxxx.com.passwdfile;
}
location ~ ^/index\.php$(/|$) {
fastcgi_pass xxxxx.com:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
When I removed error_page 405 = 200$uri; symfony is returning me a method post not allowed and when I put it nginx is returning me a 405.
I clearly missunderstand how the communication are established but don't know where I am wrong.
You are using auth_basic and auth_basic_user_file in the location / block. Am I right that you don't get a prompt to enter a password when you navigate to the side and get instead directly the 405 error?
I interpret the error to mean that an authentication is expected but doesn't happen. I would suggest to move auth_basic and auth_basic_user_file up one level in the server block. That way everything is covered and not only the one location block and you should get the password prompt.
upstream xxxx-staging {
server xxxxx.com:81 fail_timeout=0;
}
server {
listen 80;
server_name xxxxxxx.com;
return 301 https://xxxxx.com$request_uri;
}
server {
listen 443 ssl;
server_name xxxxxx.com;
root /xxxxxx/public;
auth_basic "Please authenticate";
auth_basic_user_file /etc/nginx/passwords/xxxxxxx.com.passwdfile;
ssl_certificate /etc/nginx/ssl/xxx.com.pem;
ssl_certificate_key /etc/nginx/ssl/xxx.com.key;
access_log /var/log/nginx/xxx.access.log;
error_log /var/log/nginx/xxx.error.log error;
client_max_body_size 256m;
proxy_intercept_errors on;
error_page 404 = /index.php;
error_page 405 = 200$uri;
location = / {index index.php;}
location / {
proxy_pass http://xxxxx-staging;
proxy_set_header Host $http_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-Proto $scheme;
}
location ~ ^/index\.php$(/|$) {
fastcgi_pass xxxxx.com:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
I hope that helps you.

Adding Flask app in existing nginx PHP configuration

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>').

Wordpress running on nginx on a subdomain doesn't work on port 80

I have a server hosting two Django websites and a wordpress blog, all on nginx. I have setup one Django site to be served at the top level domain, and the other at a subdomain.
I have not been able to get the Wordpress site to work when it is on port 80. If I change the port to 8079, it works correctly.
Here is my nginx conf for the Wordpress site:
server {
listen 80;
root /var/www;
index index.php index.html index.htm;
server_name sub2.mydomain.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
One thing I have noticed is that even after I change the port back to 80 and restart nginx, when I go to the url in my web browser, it will redirect to port 8079. I'm not sure if that is caused by the browser cache, or DNS or something on my server.
Here are my other nginx configs:
upstream mydomain_app_server {
server unix:/webapps/mydomain/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.mydomain.com;
client_max_body_size 4G;
access_log /webapps/mydomain/logs/nginx-access.log;
error_log /webapps/mydomain/logs/nginx-error.log;
location /static/ {
alias /webapps/mydomain/static/;
}
location /media/ {
alias /webapps/mydomain/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://mydomain_app_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/mydomain/static/;
}
}
and my last one is identical except with different paths and the following server_name:
server_name sub2.mydomain.com;

How to configure both php and nodejs app together using nginx on the same server

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.

Categories