Nginx run Proxy Pass on / and php Blog on /blog - php

I have a react application running on / on nginx. I want to enable a wordpress blog on /blog
This is my current config:
http {
upstream reactapp
{
server 127.0.0.1:3000;
}
}
server {
gzip on;
gzip_disable "msie6";
gzip_vary on;
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
root /var/www/html;
index index.php index.html index.htm;
server_name www.xxxxxxx.com;
location / {
proxy_pass http://reactapp;
}
location /blog {
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_read_timeout 3600s;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
}
I want to run wordpress on the /blog sub directory and it's located /var/www/html/blog but it only loads the root pages, all sub pages do not work, either gives 404 or goes to /.

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

wordpress nginx rewrite to wp-admin

I want to create dockerized Wordpress with nginx that will use /kokpit instead of /wp-admin I'm using nginx:latest container with default config. And declared my server block for Wordpress like below
server {
listen 443 ssl;
ssl_certificate /etc/nginx/localhost.crt;
ssl_certificate_key /etc/nginx/localhost.key;
server_name dimidia.dev.com;
root /var/www/html;
index index.php;
error_log /var/log/nginx/stderr_dimidia debug;
access_log /var/log/nginx/stdout_dimidia;
location /media
{
rewrite ^/media/(.+) /wp-content/themes/mytheme/$1;
}
location /
{
try_files $uri $uri/ /index.php?$args;
}
location /kokpit
{
rewrite ^/kokpit(.*) /wp-admin$1;
}
location ~ \.php$ {
fastcgi_read_timeout 300;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
client_max_body_size 50m;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_pass dimidia_php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
However when I reach to /kokpit files like load-scripts.php are returning 404 instead of scripts, and I cannot reach any php files inside kokpit (all are returning 404) big question is: why?

Nginx - Cant fin index.php if not under / (root) location

I have trouble to run davical (php) web calendar. There is no errol log in nginx error logs. When is calendar under \ location everything work. But when i have calendar under /calendar location. it returns 404.
default server root is: /usr/share/nginx/html/default
calendar index.php path: /usr/share/nginx/html/calendar/davical/htdocs\index.php
os: Centos 7
server {
listen 80 default_server;
server_name my_domain_name;
return 301 https://$server_name$request_uri;
}
Https
server {
listen 443 ssl http2;
server_name my_domain_name;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
ssl on;
ssl_certificate "/etc/pki/tls/certs/nginx/certificate.pem";
ssl_certificate_key "/etc/pki/tls/certs/nginx/privatekey.pem";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
ssl_dhparam "/etc/pki/tls/certs/nginx/dhparam.pem";
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
resolver 8.8.8.8 8.8.4.4;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate "/etc/pki/tls/certs/nginx/certificate.pem";
add_header Strict-Transport-Security "max-age=31536000;includeSubdomains; preload";
root /usr/share/nginx/html/default;
index index.php index.html index.htm;
include /etc/nginx/default.d/php-fpm.conf;
location /calendar {
alias /usr/share/nginx/html/calendar/davical/htdocs;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
php-fpm.conf
location ~ \.php$ {
try_files $uri =404;
fastcgi_param HTTPS on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
Your existing location ~ \.php$ block serves the /usr/share/nginx/html/default root. You need a nested location to process PHP files under the /calendar URI.
Assuming that your calendar app is designed to work within a subfolder, this may work for you:
location ^~ /calendar {
alias /usr/share/nginx/html/calendar/davical/htdocs;
index index.php;
if (!-e $request_filename) {
rewrite ^ /calendar/index.php last;
}
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
Use the ^~ modifier to prevent the other location ~ \.php$ block from taking precedence (see this document for more). Use $request_filename, as it works with alias. Avoid using try_files with alias (see this issue).

PHP assets not being served by nginx

I've installed a WP blog and added a location block for it on the nginx server block configuration. When visiting the blog at example.com/blog or any of the blog posts like example.com/blog/foo-bar-baz the content is served but all assets are missing.
When I check the logs it looks like all requests to those asset URLs are being permanently redirected to the root blog URL. Here's an example of a request from nginx's access log:
"GET /blog/wp-includes/js/jquery.min.js HTTP/1.1" 301 5 "https://example.com/blog/" "USER_AGENT_STRING"
Below is the nginx configuration:
server {
listen 80;
server_name www.example.com example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
root /home/example/current/public;
index index.html index.htm;
location ~ /blog {
index index.php index.html index.htm;
fastcgi_param SCRIPT_FILENAME /var/www/blog/index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 256 128k;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
include fastcgi_params;
}
}
How can I edit either of the location blocks to have nginx catch the requests for all URLs that contain either /blog/wp-content/ or /blog/wp-includes/? I've tried adding the following location block but it did not work:
location ~ ^(/wp-content/|/wp-includes/)/.*\.(jpe?g|gif|css|png|js|ico|pdf|m4a|mov|mp3)$ {
root /var/www/blog;
}
Your configuration does not appear to have the ability to serve static content. A more conventional approach would be to use a nested location to serve the dynamic content. For example:
location ^~ /blog {
root /var/www;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
See this document for more.

NGINX try_files rewrite causes css styles to disappear when a slash is present

So I'm trying to create a mechanism to handle friendly urls with NGINX/PHP-FPM using the basic configuration further below.
When I run an address with a non-existent uri like:
http://example.com/blah/whatever
All the styling from the default page just goes away. I thought I had a sense of what's going on and searched exhaustively, but there's nothing on stack or in the logs or that comes through in Firebug or Chrome that confirms my intuition.
Following is my nginx config:
server
listen 192.168.1.80:80 default_server;
listen 192.168.1.80:443 default_server ssl;
root /home/main/jb/www/;
client_max_body_size 30M;
# access_log logs/host.access.log main;
location / {
# try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php?$uri&$args;
index index.php;
include mime.types;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /home/main/jb/www/;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_ignore_client_abort on;
fastcgi_param SERVER_NAME $http_host;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~ /\.ht {
deny all;
}
Disclaimer: This was extracted from the question.
I seemed to have solved the problem by adding a full url to all the respective script and link tabs where before I was just using relative paths. Not sure if I fully understand the dynamics of the solution, but I hope this helps.

Categories