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;
}
}
}
Related
I have a ubuntu server at digitalocean where I am trying to get php to work.
I have successfully created a subdomain with nginx, though nginx keeps downloading the php files, instead of serving them. This is my default:
server {
listen 80;
listen [::]:80;
index index.html index.php;
server_name _;
location / {
proxy_pass http://localhost:3000;
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;
}
}
I have tried to add following to the default nginx file, with no luck:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
in completion:
server {
listen 80;
listen [::]:80;
index index.html index.php;
server_name _;
location / {
proxy_pass http://localhost:3000;
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;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
}
I am starting to think that "location / {" is targeting every content, or is there a misconfiguration somewhere? It works perfectly fine if I add html files to the subdomain, then Nginx serves them correctly, but not php.
My conf file for nginx looks like this:
server {
listen 80;
index index.php index.html;
server_name sub.example.com;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /home/simonduun/sub;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/wp-json/ {
rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
}
}
I am the beginner for nginx
I am tryingt to do
www.mywebsite.com/index.php to show in the url like www.mywebsite.com/
and
www.mywebsite.com/index.php/user/login to show in the url like www.mywebsite.com/user/login
and I got the error as show in the picture.
Here is my nginx.config
#user nobody;
worker_processes 4;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.wcp6288.com;
root /var/www/wangou;
index index.html index.htmi index.php;
#charset koi8-r;
#access_log logs/host.access.log main;
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
# Remove from everywhere index.php
if ($request_uri ~* "^(.*/)index\.php(/?)(.*)") {
return 301 $1$3;
}
}
# Remove trailing slash.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# Clean Double Slashes
if ($request_uri ~* "\/\/") {
rewrite ^/(.*) /$1 permanent;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/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$ {
root /var/www/wangou/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
try_files $uri $uri/ =404;
}
}
}
Thanks you for your helps.
Should you remove
try_files $uri $uri/ =404;
In block:
location ~ \.php$ {
root /var/www/wangou/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
try_files $uri $uri/ =404;
}
Here is my config and it's worked.
server {
listen 80;
listen 443 ssl http2;
server_name test.dev;
root "/var/www/html/test/";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
access_log off;
error_log /var/log/nginx/test.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
I am able to run node on port 8080 with pointing it to domain.com but I want to run php alongside of it.
Sample configuration is as below:
proxy_buffering on;
proxy_buffer_size 1k;
proxy_buffers 24 4k;
proxy_busy_buffers_size 8k;
proxy_max_temp_file_size 2048m;
proxy_temp_file_write_size 32k;
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;
}
}
}
Now I want to run domain.com/php as php server so any request which has domain.com/php will process php and others will work on node. Is it possible?
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;
}
}
}
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.
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;