NGINX: node.js + php on domain + subdomain - php

I'm trying to set node.js app on main domain and php-based forum on subdomain. Node.JS app works on 8000 port. Here's my config:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name myawesomeapp.ru;
location / {
proxy_pass http://127.0.0.1:8000;
access_log off;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
root /srv/myawesomeapp/static;
}
}
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name forum.myawesomeapp.ru
location / {
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Both node.js app & php forum can't be reached at myawesomeapp.ru. 127.0.0.1:8000 shows nodejs-app. What's wrong with my config? Thanks.
p.s. my php files are placed in /usr/share/nginx/html

Please include any messages you see on response of trying to visit both vhosts. As well make sure you include this setup in your nginx config as well as service nginx reload after changing configurations.
In order to proxy nginx to node you have to use upstreams. Here is configuration that might suit your needs:
upstream node {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name myawesomeapp.ru;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
access_log off;
root /srv/myawesomeapp/static
try_files $uri $uri/ =404;
expires 365d;
}
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://node/;
proxy_redirect off;
}
}
For your forum try this config:
server {
server_name www.forum.myawesomeapp.ru;
rewrite ^(.*) http://forum.myawesomeapp.ru$1 permanent;
}
server {
listen 80 default_server;
server_name forum.myawesomeapp.ru;
root /usr/share/nginx/html;
index index.php;
charset utf-8;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
access_log off;
try_files $uri $uri/ =404;
expires 365d;
}
error_page 404 /404.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_intercept_errors on;
}
}

Try just remove this lines from your config:
listen [::]:80 default_server ipv6only=on;

Related

Nginx does not redirecting to myapp url

compose up successfully and visit to the browser myapp.local, it will not load and it will go to google search. but if I type http://myapp.local it will load correctly. I think my Nginx is not doing good for redirecting. I already have in my etc/host
127.0.0.1 myapp.local
Here is my nginx
server {
listen 80;
listen [::]:80;
# For https
# listen 443 ssl;
# listen [::]:443 ssl ipv6only=on;
# ssl_certificate /etc/nginx/ssl/default.crt;
# ssl_certificate_key /etc/nginx/ssl/default.key;
server_name myapp.local;
root /var/www/myapp/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
resolver 127.0.0.11;
set $upstream php:9000;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass $upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
error_log /var/log/nginx/laravel_error.log;
access_log /var/log/nginx/laravel_access.log;
}
Thank you in advance

Nginx php5.6 fpm show blank page

I want to run node along with php on domain.com/api. My nginx configuration is as below
server {
listen 80;
server_name domain.com;
return 301 http://domain.com$request_uri;
}
server {
listen 80;
server_name domain.com;
location / {
proxy_pass http://domain.com:8080;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* \.(html|css|jpg|gif|ico|js)$ {
proxy_cache cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 301 302 30m;
expires 30m;
proxy_pass http://domain.com:8080;
}
}
location ^~ /api {
alias /var/www/html/testphp/api;
try_files $uri $uri/ #api;
location ~* \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
}
}
location #api {
rewrite ^/api/(.*)$ /api/index.php/$1 last;
}
}
But when I run file domain.com/api/test.php it gives blank page? How to solve this
I got this configuration working fully. I am running code-igniter, its working correctly.
server {
listen 80;
server_name www.domain.com;
root /var/www/html/testphp/api;
location / {
proxy_pass http://www.domain.com:8080;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* \.(html|css|jpg|gif|ico|js)$ {
proxy_cache cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 301 302 30m;
expires 30m;
proxy_pass http://www.domain.com:8080;
}
}
location /api/ {
alias /var/www/html/testphp/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}

How to run django and wordpress on NGINX server using same domain?

I have tried many ways but do not know how to run Django on example.com and wordpress on example.com/blog
The following running project directory structure for Django and Wordpress.
Django app dir- /home/ubuntu/django
Django app running successfully on - example.com:8000
Wordpress dir - /var/www/html/blog
Wordpress running successfully on - example.com
Nginx configuration
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html/blog;
index index.php index.html index.htm;
server_name example.com;
location / {
# try_files $uri $uri/ =404;
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/html;
}
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;
}
}
Note- Django app running by gunicorn, I know the subdomain may be the solution but I do not want that.
How to write nginx configuration for both Wordpress and Django to run Django app on example.com and Wordpress on example.com/blog ?
Thanks alex for helping me out to solve this problem.
Here is the solution
Django app dir- /home/ubuntu/django
Wordpress dir - /var/www/html/blog
NGINX Conf file
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name example.com;
location / {
proxy_pass http://127.0.0.1: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;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ /blog/.*\.php$ {
root /var/www/html;
index index.php index.html index.htm;
set $php_root /var/www/html/blog;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /blog {
root /var/www/html;
index index.php index.html index.htm;
set $php_root /var/www/html/blog;
try_files $uri $uri/ /blog/index.php;
}
location /static/ {
alias /home/ubuntu/django/your_app_name/static/static_root/;
}
location /media/ {
alias /home/ubuntu/django/your_app_name/media/ ;
}
}
Note- please replace your home and siteurl with http://example.com/blog in wp-location table of wordpress
Now Your Django app running on example.com
Now Your Blog running on example.com/blog
As i understand you have a server running your django site and one running your wordpress site. if so you can do something like this:
{
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name example.com;
access_log /var/log/nginx/example-access.log;
location / {
proxy_pass http://127.0.0.1:3001;
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-Proto $scheme;
}
location /blog/ {
proxy_pass http://127.0.0.1:(wordpress port);
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-Proto $scheme;
}
}
if you need to server the php blog from ngix no apache in the middle use somethon like:
location ~ /blog/.*\.php$ {
root /var/www/html/blog;
index index.php index.html index.htm;
set $php_root /var/www/html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
Just add these lines to your config file.
location ~ /blog/.*\.php$ {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location /blog {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
try_files $uri $uri/ /blog/index.php;
}

How do make clean urls in nginx?

I'm trying to make the urls for nginx (Ubuntu 14.04) clean, instead of example.com/profile.php it would be example.com/profile, or example.com/about.html to example.com/about
I have tried pretty much all the other ones on stackoverflow however they do not work, including making the site have a 500 internal server error, to making me download the PHP file
Current config
server {
listen 443 ssl;
root /usr/share/nginx/html;
index index.php index.html index.htm;
include /etc/nginx/mime.types;
server_name mydomain.com;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location / {
try_files $uri.html $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name mydomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name api.mydomain.com;
location / {
proxy_pass https://mydomain:1337;
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_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128k;
}
}
server {
listen 1337 ssl;
server_name api.mydomain.com;
root /usr/share/nginx/api;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
}
Use the try_files [1] directive.
location / {
try_files $uri.php $uri.html =404;
}
In this example, if the browser was requesting /folder/file, it would first try to find file.php and then file.html before giving up and throwing a 404 error.
If that doesn't work, you can use an if statement instead:
location / {
if (-f $request_filename.php) {
rewrite ^ $uri.php;
break;
}
if (-f $request_filename.html) {
rewrite ^ $uri.html;
break;
}
}
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

Wordpress not loading on shared NGINX server name

I am successfully running a Wordpress installation alongside Meteor using NGINX. I have no real experience with Wordpress or php, so this may be a simple fix.
The following configuration works:
server_tokens off;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream mydomain-production {
server localhost:8000;
}
# redirect all non-subdomain traffic to https
server {
listen 80;
server_name www.mydomain.com mydomain.com;
rewrite ^ https://$host$request_uri? permanent;
access_log /var/log/nginx/mydomain.access.log;
error_log /var/log/nginx/mydomain.error.log;
}
# this non-subdomain serves meteor correctly
server {
listen 443 ssl spdy;
server_name www.mydomain.com mydomain.com;
access_log /var/log/nginx/mydomain.secure.access.log;
error_log /var/log/nginx/mydomain.secure.error.log debug;
ssl_certificate #...removed...# ;
ssl_certificate_key #...removed...# ;
ssl_stapling on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers #...removed...# ;
add_header Strict-Transport-Security "max-age=31536000;";
################################
# additional code will go here #
################################
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://mydomain-production;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
if ($uri != '/') {
expires 30d;
}
}
}
# this temporary subdomain serves wordpress correctly
server {
listen 80;
server_name wordpress.mydomain.com;
access_log /var/log/nginx/mydomain.access.log;
error_log /var/log/nginx/mydomain.error.log;
index index.php index.html index.htm;
root /var/www;
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/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
So since I have Wordpress functioning on a temporary subdomain, I want to make it work on the same domain as Meteor and include location directives to route certain requests to Wordpress instead.
I tried adding the following to the 443 server name:
# additional code
location /blog {
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;
index index.php index.html index.htm;
}
location /wp-admin {
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;
index index.php index.html index.htm;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}
After doing this, I get an NGINX 404 page at mydomain/blog. So the location directive is successfully sending the request to /var/www instead of Meteor, but for some reason it is not getting to the Wordpress router. I have linked my NGINX error debug output here.
This was somehow solved by simply moving the root /var/www; and index index.php index.html index.htm; outside of the location directive(s). I would be interested to know why this is necessary if anyone can shed any light on this.

Categories