I am trying to run reactjs application and PHP application together on Nginx.
Reactjs application runs well, but I am unable to run PHP application.
I have added two server blocks for both the applications but unable to run PHP app which is actually my api/service for react js app.
Any help in this regard would be much appreciated.
Here is my nginx configuration for react js application:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/qurancom-reactjs;
index index.html index.htm index.nginx-debian.html;
server_name 3.16.130.108;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
and here are my php app nginx server block configurations:
server {
listen 80;
listen [::]:80;
root /var/www/html/quran-app-services/api;
index index.php index.html index.htm index.nginx-debian.html;
server_name 3.16.130.108;
location / {
try_files $uri $uri/ =404;
}
}
Thanks
I am trying to create a wordpress website that uses nginx as a reverse proxy to apache - apache serves only php requests.
I was able to successfully install wordpress but whenever I try to open the website it says "too many redirects". However when I try to open example.com/wp-admin it loads and I am able to login into and access the admin panel.
My /etc/nginx/sites-available/webproxy is:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name server_ip;
location / {
try_files $uri /index.php;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
deny all;
}
}
I checked both apache and nginx error logs - empty.
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.
I have the following nginx config
server {
listen 80;
client_max_body_size 2M;
server_name some.app;
root /var/virtual/a-cakephp-app/webroot;
access_log /var/log/nginx/a-cakephp-app-access.log;
include common.conf;
include cakephp.conf;
location /billing/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:89;
proxy_redirect off;
rewrite ^/billing/(.*)$ /$1 break;
}
And my webapps are:
/var/virtual/a-cakephp-app ==> virtual path that leads to the cakephp folder (definitely working)
/var/virtual/a-laravel-app ==> virtual path that leads to the laravel folder (not too sure how to test it)
What I want to achieve
I have a cakephp 2 app that is running at http://some.app. What I want is to start another app running Laravel at http://some.app/billing
My Laravel .env
APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:somekey
APP_URL=http://some.app/billing
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
What did I get
I got a bad gateway error
What did I expect
I was hoping that the laravel app can work without compromising the cakephp app
It is generally a bad idea to mix and match independent apps on a single domain, which may lead to security issues w.r.t. cookies.
You're trying to use a proxy_pass for Laravel, a PHP framework. Note that this directive is meant to be used when the request is meant to be passed, in HTTP over TCP form, to a subsequent HTTP server, running on the specified port (e.g., Apache Tomcat, Jetty etc).
PHP (and hence Laravel) can be run by nginx itself, and doesn't require a separate server, so, your proxy_pass setup is likely a mistake, and should have been an appropriate fastcgi_pass set of directives instead (the normal way php is executed from within nginx).
We have no idea what is in the files that you include, however, it would be a good guesstimate that they do contain a location ~ \.php$ directive (for handling the php files of your existing php app).
Note that as per http://nginx.org/r/location, a location with a regular expression like location ~ \.php$ will take precedence over a prefix-string location like location /billing/, when a file like /billing/index.php is accessed.
To modify such behaviour, use the ^~ specifier for the prefix string location, e.g., location ^~ /billing/.
In summary, I'd use a separate domain. Else, use location ^~ /billing/, and fit all the proper fastcgi within.
You need to tell nginx that the root is different for /billing/ paths:
...
location /billing/ {
...
root /var/virtual/a-laravel-app/public;
# this replaces the rewrite
# rewrite will alter the url in nginx and a new lookup will be made
# the entry point for Laravel is the public/index.php file
try_files /index.php =404;
....
}
...
I kept the original config as such:
server {
listen 80;
client_max_body_size 2M;
server_name some.app;
root /var/virtual/a-cakephp-app/webroot;
access_log /var/log/nginx/a-cakephp-app-access.log;
include common.conf;
include cakephp.conf;
location /billing/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:89;
proxy_redirect off;
rewrite ^/billing/(.*)$ /$1 break;
}
Then I created this config after that:
server {
listen 89;
client_max_body_size 2M;
server_name 127.0.0.1;
root /var/virtual/a-cakephp-app/another-laravel-app/public;
include common.conf;
include cakephp.conf;
}
Note the port 89 does not matter so long as the server is not using it for some other app
Ideally you should not mix all the configurations under one file. nginx.conf should only only contain universal configurations like gzip being on, not to give out server tokens, etc. The individual file should be under sites-enabled folder
On why the bad gateway error is coming is maybe because you should have one root and then multiple location blocks to handle the same.
Also, once inside the billing block, are you trying to rewrite it to remove the billing folder? Why?
I was following this tutorial https://www.digitalocean.com/community/articles/how-to-configure-nginx-as-a-front-end-proxy-for-apache to have a setup where nginx handles static stuff and all php files ares handled by apache on a fresh ubuntu box.
Everything went smoothly and I installed a php script on the server.
However, it is not loading any .html files. All .php files are loading and working fine. Whenever browser requests a html file, the browser kind of redirects back to index.php (although the url ending with .html remains in the address bar)
I have double checked and I've done everything according to the tutorial. What might prevent nginx from loading html files?
This is my nginx config:
server {
listen 80;
root /var/www/;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
deny all;
}
Below is the nginx error log:
http://d.pr/f/voxs