Ive looked at this for hours, but am still not getting where I need to get to despite doing a lot of hours googling.Nginx is throwing no input file specified
my config in /etc/nginx/sites-enabled/siteb looks like:
upstream site {
#flask app
server 127.0.0.1:8001;
}
upstream siteb-blog {
#wordpress php
server unix:/var/run/php5-fpm.sock;
}
server {
server_name siteb.com;
listen 80;
root /home/www/flask-deploy/siteb;
location ~* ^blog/ {
try_files $uri $uri/ /blog/index.php?$query_string;
root /home/www/flask-deploy/siteb-blog;
location ~ \.php$ {
fastcgi_pass siteb-blog;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location / {
try_files #proxy #proxy;
}
location #proxy {
internal;
proxy_pass http://siteb;
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;
}
location /static {
alias /home/www/flask-deploy/siteb/static/;
}
}
However, when i try to access mysite.com/blog or mysite.com/blog/info.php i get a 404 error.
ive checked error.log and it doesnt show any errors. access.log, just shows:
[16/Dec/2016:16:11:56 -0500] "GET /blog/index.php HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Ubuntu; Linux armv7l; rv:50.0) Gecko/20100101 Firefox/50.0"
My siteb.com works fine. siteb.com/blog does not.
im specifically trying to get siteb-blog working at siteb.com/blog.
Thank you in advance!
You have WordPress installed under a directory called siteb-blog and would like to access it with a URI prefix of /blog/. One of your problems is that the $request_filename is constructed from the root and URI, so that the /blog segment becomes part of the pathname. See this document for details.
The simple solution with static files is to use the alias directive, but I try to avoid using it with try_files because of a long standing bug.
My preferred solution is to silently (using internal rewrite) map the /blog URI to /siteb-blog, then continue to use root as normal.
For example:
server {
server_name siteb.com;
listen 80;
location /blog {
rewrite ^/blog(.*)$ /siteb-blog$1 last;
}
location /siteb-blog {
root /home/www/flask-deploy;
try_files $uri $uri/ /blog/index.php?$query_string;
location ~ \.php$ {
try_files $uri /blog/index.php;
fastcgi_pass siteb-blog;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location / {
proxy_pass http://siteb;
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;
}
location /static {
root /home/www/flask-deploy/siteb;
}
}
You can make the location /siteb-blog internal, by adding the internal directive. This will make it inaccessible directly.
A root statement in both the server block and the location /static block is unnecessary - it only needs to appear once.
The alias was unnecessary - root is preferred. See this document for details.
The named location was unnecessary - merged with location /
Finally, WordPress needs to know that it is hosted under /blog, otherwise it cannot find its resource files (css & js). You will need to set the values for WP_SITEURL and WP_HOME either using wp-config.php or using the WordPress ⇒ DashBoard ⇒ Settings ⇒ General page.
Related
I have few virtual hosts (sites) running on this single server.
Right now, on this root virtualhost i have a forum (running on Docker) but served by Nginx mysite.com and I have its AMP pages being served on /amp route which is
mysite.com/amp. These AMP pages are basically 1 single index.php file & they all are handled by this 1 file. These are served by PHP using Nginx.
What I want is, when a user hits any of these requests matching below patterns: (like if ANY URL on this domain ending with ?amp=1
mysite.com?amp=1
mysite.com/t/my-topic/121?amp=1
mysite.com/c/CategoryCaseInsensitive/13?amp=1
mysite.com/u/john?amp=1
mysite.com/u/john/summary?amp=1
THEN
I want to redirect this request and send it to my AMP page (which is running on PHP file and will be then served/handled by index.php which is present in /var/www/amp ) . Right now the PHP code is being served on /amp but i want to serve it on mysite.com?amp=1 so any URL preceding ?amp=1
I have tried this code but its not seem to working for all cases:
#if ($arg_amp) {
# return 302 /amp$request_uri;
#}
Below is my current NGINX config file for this virtual host:
#Vhost Config Server, serving Ruby on Rails App on Docker on domain root
server {
listen 443 ssl http2;
ssl on;
ssl_certificate /var/www/cert/mysite.pem;
ssl_certificate_key /var/www/cert/mysite.key;
server_name mysite.com www.mysite.com;
location / {
proxy_ssl_server_name on;
proxy_pass http://localhost:PORT;
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_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#if ($arg_amp) {
# return 302 /amp$request_uri;
#}
}
#Serving PHP code on /AMP route
location #amp {
rewrite ^/amp(.*) /amp/index.php?q=$1;
}
#will match any prefix for amp, amping, or amp/anything/any
location /amp {
index index.php;
try_files $uri $uri/ #amp;
alias /var/www/amp;
#PHP config for Nginx
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
#/amp route ends
}
Is this not possible in Nginx?
I think it could be done more simple way. If your index.php is the only PHP file needs to be served, you don't need this whole location ~ \.php$ { ... } thing.
server {
...
location / {
if ($arg_amp) {
return 302 /amp$request_uri;
}
...
}
location /amp {
# if you really need an original URI as a query argument
rewrite ^/amp(.*) /amp/index.php?q=$1 break;
include fastcgi_params;
# hardcode script path
fastcgi_param SCRIPT_FILENAME /var/www/amp/index.php;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
Or you can skip that rewrite rule at all, just get an original request URI from $_SERVER['REQUEST_URI'] and strip /amp prefix from it within your PHP code:
location /amp {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/amp/index.php;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
You can even process those requests with your script without 302 redirects:
server {
...
location / {
if ($arg_amp) {
rewrite ^ /amp$request_uri last;
}
...
}
location /amp {
internal;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/amp/index.php;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
An original URI would be accessible from $_SERVER['REQUEST_URI'].
Facing redirection issue while setting up Wordpress with Nginx via Amazon API Gateway and Network Load Balancer.
Description:-
We have our main website xyz.com(served by Amazon API Gateway and Load Balancer) and want our blogs to be present on xyz.com/blogs.
So, we have set up Amazon API Gateway and Load Balancer to redirect any request of the for xyz.com/blogs to the EC2 containing Wordpress with Nginx.
Problem:-
The problem that we are facing is, the home page is rendered fine but when we try to render any other page, e.g:- xyz.com/blogs/my-first-post/ or xyz.com/blogs/wp-admin then it gets stuck over there and nothing comes as response. As a part of our initial debugging, we found out that Wordpress is making redirections to the Network Load Balancer url, (which as per our guess) is not accessible and we are not getting any response.
This is how our default nginx conf looks like (/etc/nginx/conf.d/xyz_blogs.conf), which we got from this link => Wordpress|Nginx
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
## Your website name goes here.
server_name xyz.com;
## Your only path reference.
root /var/www/html;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
How shall we resolve this issue?
A prior thanks for any help given hereby.
Thy this:
upstream php-app { # <-- change name of the upstram
# server unix:/tmp/php-cgi.socket; # <-- Remove this
# List of [IP:Port] OR list of [sockets] not both mixed
server 127.0.0.1:9000;
# server 127.0.0.1:9001; # example
# server 127.0.0.1:9002; # example
}
server {
listen 80 default_server; # <-- Add this line
listen [::]:80 default_server; # <-- Add this line
server_name xyz.com *.xyz.com;
root /var/www/html;
# index index.php; # <-- Will be removed, you app is provided by the socket/port
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location / { # Put this at the end, NGinx apply priority
proxy_pass http://php-app; # Add this line
}
}
Update 1:
update config
...
server_name xyz.com *.xyz.com *.amazonaws.com;
...
Sorry for the late post, but currently we have implemented this using a solution which might not be much scalable. What we have done is we have deployed our website and wordpress in the same machine but in a different containers. So, our website is running in a different container and wordpress is running in a different container but both are present in the same EC2.
And we are using nginx configuration in wordpress to redirect the requests to the fellow container. Currently my nginx configuration looks something like this:
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
listen 80;
server_name xyz.com;
root /var/www/html;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
index index.php;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
location / {
rewrite ^/(.*) /$1 break;
proxy_pass http://xyz-container:80;
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;
}
}
It would have been great if somehow we would have been able to deploy wordpress container in a different EC2 and still achieve this.
It will really be helpful and a lot better if someone can suggest a better scalable working solution for the problem with wordpress container in a different EC2.
Thanks.
My server is frequently having Timed Out problem, I have adjust parameters such as fastcg_read_timeout and proxy_read_timeout but it did not solve my problem. Find below my xxxx.conf
server {
server_name www.xxxx.com;
return 301 $scheme://xxxx.com$request_uri;
}
server {
## Your website name goes here.
server_name xxxx.com;
## Your only path reference.
root /var/www/xxxx.com;
# This should be in your http block and if it is, it's not needed here.
index index.html index.php;
error_log /var/log/nginx/xxxx.com-error.log;
access_log /var/log/nginx/xxxx.com-access.log;
# Body size (max upload)
# client_max_body_size 64m;
# client_body_buffer_size 2m;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}
# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ /index.php?$args;
# This to stop connection timeout
proxy_http_version 1.1;
proxy_set_header Connection "";
# Time-out Settings
proxy_send_timeout 150;
proxy_read_timeout 150;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm-sock;
fastcgi_read_timeout 150;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}}
I experienced this same issue while developing on my local nginx machine.
After some extensive research I found a solution to my problem (Unfortunately I cannot find the article anymore).
You could try changing this:
fastcgi_pass_unix: localhost:9000;
Then run a 'sudo service nginx reload'.
This fixed the issue for me. Hopefully It will too for you.
This problem resolved for me. I commented on the proxy_send_timeout and proxy_read_timeout and fastcgi_read_timeout but leave proxy_http_version and proxy_set_header in the codes below.
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
# proxy_send_timeout 150;
# proxy_read_timeout 150;
}
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.
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