I'm serving wordpress for most urls, and static content for a lot of legacy content. Everything under one domain name. Wordpress is served out of docker, via a proxy pass. Static files are just from a unique root.
location #docker {
proxy_pass http://127.0.0.1:8007;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
}
location / {
include /etc/nginx/snippets/maritime_static_site.conf;
root /mnt/code/maritime/maritime-static;
#try_files $uri $uri/index.html $uri/index.htm $uri/index.php #docker;
try_files $uri #docker;
auth_basic "Private Beta";
auth_basic_user_file /etc/nginx/.htpasswd-maritime;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Static html, static php, and images are working. Wordpress is working. Wordpress admin (/wp-admin & wp-login.php) are giving file not found. I'm guessing it's trying files from the static root location. Any ideas how I can fix this?
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'].
I'm new to nginx, and trying to move a wordpress website on it.
Problem is I need to run a file called "installer.php", and nginx shows a 404 error for it (from domain/rocketstack/installer.php).
Incase I add a specific "location" directive, I get returned a "No input file specified" error (not sure I'm doing this right).
Accessing domain/rocketstack/index.php directly returns the same 404, but works if I go to domain/rocketstack/ (this is fine I guess).
I'm using php7.2-fpm on ubuntu 18.04, "installer.php" is in /var/www/rocketstack/, has permission 644. cgi.fix_pathinfo=0 is set in php.ini.
To set up the environment I used this guide: https://www.wpintense.com/2018/10/20/installing-the-fastest-wordpress-stack-ubuntu-18-mysql-8/
Here's my /etc/sites-available/rocketstack.conf file
How can I fix this? I've lost so many hours on this! Yet it must be so simple! Thank you so much
# This config file uses nginx fastcgi-cache
fastcgi_cache_path /var/www/cache levels=1:2 keys_zone=rocketstack:100m inactive=60m;
server {
listen 80;
listen [::]:80;
server_name _;
root /var/www/rocketstack;
index index.php index.htm index.html;
access_log /var/log/nginx/rocketstack_access.log;
error_log /var/log/nginx/rocketstack_error.log;
include snippets/acme-challenge.conf;
# Exclusions
include snippets/exclusions.conf;
# Security
include snippets/security.conf;
# Static Content
include snippets/static-files.conf;
# Fastcgi cache rules
include snippets/fastcgi-cache.conf;
include snippets/limits.conf;
include snippets/nginx-cloudflare.conf;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ (^|/)\. {
return 403;
}
location ~/installer.php {
root /var/www/rocketstack/;
fastcgi_index installer.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include snippets/fastcgi-params.conf;
include fastcgi.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~ \.php$ {
try_files $uri =404;
include snippets/fastcgi-params.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
# Skip cache based on rules in snippets/fastcgi-cache.conf.
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
# Define memory zone for caching. Should match key_zone in fastcgi_cache_path above.
fastcgi_cache rocketstack;
# Define caching time.
fastcgi_cache_valid 60m;
#increase timeouts
fastcgi_read_timeout 6000;
fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
proxy_read_timeout 6000;
proxy_connect_timeout 6000;
proxy_send_timeout 6000;
send_timeout 6000;
#these lines should be the ones to allow Cloudflare Flexible SSL to be used so the server does not need to decrypt SSL
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
}
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server ;
server_name _;
root /var/www/rocketstack;
index index.php index.htm index.html;
access_log /var/log/nginx/rocketstack_ssl_access.log;
error_log /var/log/nginx/rocketstack_ssl_error.log;
#once you have SSL certificates using LetsEncrypt you can alter the paths in the two lines below to reflect your domain and uncomment the lines by removing the leading # symbol
#ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# Exclusions
include snippets/exclusions.conf;
# Security
include snippets/security.conf;
# Static Content
include snippets/static-files.conf;
# Fastcgi cache rules
include snippets/fastcgi-cache.conf;
include snippets/limits.conf;
include snippets/nginx-cloudflare.conf;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include snippets/fastcgi-params.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
# Skip cache based on rules in snippets/fastcgi-cache.conf.
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
# Define memory zone for caching. Should match key_zone in fastcgi_cache_path above.
fastcgi_cache rocketstack;
# Define caching time.
fastcgi_cache_valid 60m;
#increase timeouts
fastcgi_read_timeout 6000;
fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
proxy_read_timeout 6000;
proxy_connect_timeout 6000;
proxy_send_timeout 6000;
send_timeout 6000;
#these lines should be the ones to allow Cloudflare Flexible SSL to be used so the server does not need to decrypt SSL if you wish
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
}
}
Fixed by trial and error. Current setting:
location ~ \installer.php {
try_files $uri $uri/ /installer.php?$args;
fastcgi_index installer.php;
include snippets/fastcgi-params.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
Problem was I wasn't telling nginx where to pick installer.php, I achieved that with try_files. Root setting in location was redundant too.
I try to run node js and php on the same domain with nginx. However the location for php ("/ajax") does not work. I always get the message "File not found.". The nginx logs print
The URL is http://localhost:8085/ajax so far, the scripts are located at /var/www/public
The folder /ajax does NOT exist (none of the paths do, as everthing shall be redirected to /var/www/public/index.php)
nginx | 2017/08/27 20:47:48 [error] 6#6: *6 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.23.0.1, server: localhost, request: "GET /ajax HTTP/1.1", upstream: "fastcgi://172.23.0.4:9000", host: "localhost:8085"
nginx | 172.23.0.1 - - [27/Aug/2017:20:47:48 +0000] "GET /ajax HTTP/1.1" 404 27 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36" "-"
This is my configuration, what do I have to change?
upstream react {
server react:3000;
keepalive 8;
}
server {
listen 0.0.0.0:80;
server_name localhost;
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 $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://react;
proxy_redirect off;
}
location /ajax {
index index.php index.html;
root /var/www/public;
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;
}
client_max_body_size 5m;
}
I tried
fastcgi_param SCRIPT_FILENAME /var/www/public$fastcgi_script_name;
as suggested in some threads, but that does not work
So I have trying to work on your Issue for last 3 days, to get a better and deeper understanding of FASTCGI. I put a logger between and NGINX and FPM, and that helped me better understand the interactions. Below is the config that I believe should work for you
Edit-1
Updated config after resolving issue on chat
upstream react {
server react:3000;
keepalive 8;
}
map $fastcgi_script_name $fastcgi_script_name_fcgi {
"~*/ajax/(?P<rest_url>.*)$" /$rest_url;
default $fastcgi_script_name;
}
map $request_uri $request_uri_fcgi {
"~*/ajax/(?P<rest_url>.*)$" /$rest_url;
default $request_uri;
}
map $document_uri $document_uri_fcgi {
"~*/ajax/(?P<rest_url>.*)$" /$rest_url;
default $document_uri_fcgi;
}
server {
listen 0.0.0.0:80;
server_name localhost;
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 $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://react;
proxy_redirect off;
}
location /ajax {
alias /var/www/public;
try_files $uri #php;
}
location #php {
fastcgi_split_path_info ^/ajax/(.+\.php)(/.+)$;
fastcgi_pass fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param REQUEST_URI $request_uri_fcgi;
fastcgi_param DOCUMENT_URI $document_uri_fcgi;
fastcgi_param SCRIPT_FILENAME /var/www/public/index.php;
}
client_max_body_size 5m;
}
So the basic idea is to replace /ajax from document_root using the alias directive. Then to override the request_uri and other parameters so the correct urls reach the PHP code for route resolution.
Below is part of what my php script receives when I call http://vm/ajax/router.php?x=2
{
"COOKIES": null,
"GET": {
"x": "2"
},
"POST": [],
"REQUEST": {
"x": "2"
},
"HEADERS": null,
"SERVER": {
"HOME": "/var/www",
"SCRIPT_FILENAME": "/var/www/html/router.php",
"DOCUMENT_ROOT": "/var/www/html",
"DOCUMENT_URI": "/router.php",
"REQUEST_URI": "/router.php?x=2",
"SCRIPT_NAME": "/router.php",
"CONTENT_LENGTH": "",
"CONTENT_TYPE": "",
"REQUEST_METHOD": "GET",
"QUERY_STRING": "x=2",
"FCGI_ROLE": "RESPONDER",
"PHP_SELF": "/router.php",
"argv": [
"x=2"
],
"argc": 1
}
}
As you can see nothing gets /ajax
The current configuration works for me, however /ajax is passed to php-fpm. I was not able to get rid of the /ajax prefix in all variables like REQUEST_URI
upstream react {
server react:3000;
keepalive 8;
}
server {
listen 0.0.0.0:80;
server_name localhost;
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 $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://react;
proxy_redirect off;
}
location /ajax {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/public$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
client_max_body_size 5m;
}
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.
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.