Subdirectory configuration for wordpress using nginx - wordpress

I'm trying to serve WordPress from the subdirectory site.com/blog but im facing 404, tried different configurations but none of them are working getting a 404.
The WordPress files are at /home/ubuntu/wordpress
Here is my Nginx configuration, I'm serving static files for the root domain site.com and running Laravel for backend API, I think it conflicts with the FastCGI settings, not sure though.
server {
root /home/ubuntu/mysite/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name mysite.com www.mysite.com;
access_log off;
include /etc/nginx/badbots.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|json|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
expires 365d;
add_header Cache-Control "public, max-age:24h";
}
large_client_header_buffers 4 32k;
location /blog {
alias /home/ubuntu/wordpress;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
listen 80;
}

Related

How to make the wp-admin access from a different subdomain?

------- Technologies I'm using ---------
Debian 10
Nginx server
PHP8.0
--------- The scenario ------------
I have exmaple.com which has an HTML static website
and I have a subdomain, blog.example.com I installed WP on it and it's working fine
but I want to make the wp-admin access on login.blog.example.com.
-------- What I tried -------------
1- I tried redirecting any /wp-* URL to login.blog.example.com, but that isn't useful if there are no files/folders of wp-admin on login.blog.example.com.
2- I followed this, but it wasn't handy since they are redirecting to 404 and a static page
https://403.ie/how-to-serve-wp-admin-from-a-separate-subdomain/
-------- The nginx configuration -----------
example.com:
server{
listen 80;
listen [::]:80;
server_name IP_ADDRESS;
return 301 http://example.com;
}
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
blog.example.com:
server {
listen 80;
listen [::]:80;
server_name blog.example.com;
root /var/www/blog.example.com;
index index.html index.php index.htm;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
# location /wp-admin{
# rewrite ^/wp-(.*)$ http://login.blog.example.com redirect;
# }
location ~ \.php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The first step is to create a server to handle requests to login.blog.example.com. This is identical to blog.example.com except for the server_name and any "login" bits you want to add.
Then add a redirect in the blog.example.com server block:
location ^~ /wp-admin {
return 302 $scheme://login.blog.example.com$request_uri;
}
WordPress may insist on redirecting you back to blog.example.com. This is because of the settings for site URL in the Dashboard configuration or the wp-config.php configuration file.
You should be able to drop the hostname from the HOME and SITEURL values and use / instead of http://blog.example.com.

NGINX show default image if not found

I have this NGINX conf file,
If I load any root url, it will show my default.jpg image,
but all of my mail images are stores in subfolders, eg:
/missing.jpg // works
/images/mising.jpg // shows nginx 404 not /default.jpg
Here is my nginx conf.
Note: either I can use: /default.jpg or /index.php which outputs the same image..
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php;
server_name _;
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
add_header Cache-control "public";
access_log off;
expires 365d;
}
location / {
#try_files $uri $uri/ default.jpg;
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
I solved this by adding the try_files to the image cache block.
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
add_header Cache-control "public";
access_log off;
expires 365d;
try_files $uri /index.php$is_args$args;
}

How to remove index,php from CI project url to make clean url in nginx Hostwinds server, when CI project is inside subdirectory?

"I'm setting up an Nginx server on Hostwinds for CI project which is in a subdirectory. Where I want to make a clean URL by removing index.php. Initially, this project was running on Apache server and with the help of .htaccess file, I have made a clean URL by removing index.php. But .htaccess file not works on the Nginx server. So, tell me what codes should I use to remove index.php from URL in 'Hostwinds' Server.
In subdirectory home page of projects opens but when you click on any of its links it will redirects you to 404 page.
I have tried various solution which is available on the internet but none of them worked for me. I have used this code inside nginx.conf file.
some of them:-
1)
location /category/subcategory {
try_files $uri $uri/ /category/subcategory/index.php;
}
2)
location /subfoldername/ {
root /usr/share/nginx/www/subfoldername;
try_files $uri $uri/ /index.php?$query_string;
}
3)
location /api/ {
alias /var/www/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass backend;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
4)
location /nested {
alias /var/www/nested/public;
try_files $uri $uri/ /index.php$is_args$args;
}
...http {...
server {
listen 443 ssl http2;
#listen [::]:443 ssl http2 ipv6only=off;
server_name example.com;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ #backend;
}
location #backend {
include proxy_params_common;
# === MICRO CACHING ===
# Comment the following line to disable 1 second micro-caching for dynamic
HTML content
include proxy_params_dynamic;
}
# Enable browser cache for static content files (TTL is 1 hour)
location ~* \.(?:json|xml|rss|atom)$ {
include proxy_params_common;
include proxy_params_static;
expires 1h;
}
# Enable browser cache for CSS / JS (TTL is 30 days)
location ~* \.(?:css|js)$ {
include proxy_params_common;
include proxy_params_static;
expires 30d;
}
# Enable browser cache for images (TTL is 60 days)
location ~* \.(?:ico|jpg|jpeg|gif|png|webp)$ {
include proxy_params_common;
include proxy_params_static;
expires 60d;
}
# Enable browser cache for archives, documents & media files (TTL is 60 days)
location ~* \.
(?:3gp|7z|avi|bmp|bz2|csv|divx|doc|docx|eot|exe|flac|flv|gz|less|mid|midi|mka|mkv|mov|mp3|mp4|mpeg|mpg|odp|ods|odt|ogg|ogm|ogv|opus|pdf|ppt|pptx|rar|rtf|swf|tar|tbz|tgz|tiff|txz|wav|webm|wma|wmv|xls|xlsx|xz|zip)$ {
set $CACHE_BYPASS_FOR_STATIC 1;
include proxy_params_common;
include proxy_params_static;
expires 60d;
}
# Enable browser cache for fonts & fix #font-face cross-domain restriction (TTL is 60 days)
location ~* \.(eot|ttf|otf|woff|woff2|svg|svgz)$ {
include proxy_params_common;
include proxy_params_static;
expires 60d;
#add_header Access-Control-Allow-Origin *;
}
# Prevent logging of favicon and robot request errors
location = /favicon.ico {
include proxy_params_common;
include proxy_params_static;
expires 60d;
log_not_found off;
}
location = /robots.txt {
include proxy_params_common;
include proxy_params_static;
expires 1d;
log_not_found off;
}
# Deny access to files like .htaccess or .htpasswd
location ~ /\.ht {
deny all;
}
}
Initially link:- www.example.com/index.php/search-result
resulted in link:- www.example.com/search-result

Nginx give access to subdirectory

We're running a Wordpress site on a Nginx server, I am now trying to install Piwik there in the /stats folder.
This is the default Nginx configuration that came with the install, I added the "/stats" block myself, but it doesn't work - it gets rendered by WordPress whenever I go to mysite.com/stats instead of going to that folder.
Desired behavior would be that the /stats subdirectory (and all files and directories in it) is just parsed by PHP as would be on a default install without Nginx rules
Any clue what I'm missing?
server_name _;
port_in_redirect off;
client_header_buffer_size 4k;
client_body_buffer_size 128k;
client_max_body_size 16m;
root /var/www/html;
index index.html index.php;
charset utf-8;
log_not_found off;
gzip_static on;
gzip_types text/css application/javascript text/xml;
gzip_vary on;
gzip on;
# redirect server error pages to the static page /50x.html
#
error_page 500 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location #rewrite {
rewrite ^.*$ /index.php?$args;
}
error_page 404 #rewrite;
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# WP Multisite rewrites
rewrite /([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 last;
rewrite /([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 last;
location / {
try_files $uri $uri.gz $uri/ #rewrite;
}
location ~ \.sql$ {
rewrite ^.*$ /index.php?$args;
}
# We do not want to run php from wp uploads
location ~* /(?:uploads|files)/.*\.php$ {
rewrite ^.*$ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
}
location /stats {
try_files $uri $uri/ /index.php?$args;
index index.php;
}
location = /favicon.ico {
access_log off;
expires 2w;
add_header Cache-Control public;
try_files $uri #rewrite;
}
location ~* \.(js|css|jpg|jpeg|png|gif|ico|woff|woff2|ttf|otf|eot|pdf|xml|mp4|ogg|mp3|mov|wmv|avi|cur|rtf|txt|swf)$ {
add_header Cache-Control public;
add_header Access-Control-Allow-Origin *;
expires 2w;
try_files $uri $uri.gz;
}
The rules for multi-site WordPress, particularly this one: rewrite /([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 last; will redirect any /stats/index.php URI back to WordPress's /index.php.
If you are not using a multi-site WordPress, you can safely delete the redundant rewrite rules.
If you are using a multi-site WordPress, some redesign is required.

Magento Wordpress Nginx Configuration

I have a Magento shop at http://example.com and I want to keep a Wordpress blog at http://example.com/blog.
I have installed the blog and everything seems to be fine but when am logging to Wp-Admin am getting 404 for css and js files due to which dashboard is looking very ugly.
Am I doing any mistake? am attaching my nginx config file
##################################################################################
#
# example.com
#
##################################################################################
server {
listen 80;
server_name example.com ;
#charset koi8-r;
#access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
root /usr/share/nginx/html/mebozo-magento.mebozo.com;
try_files $uri $uri/ #handler; ## If missing pass the URI to Magento's front handler
index index.php index.html index.htm;
}
location /blog {
root /usr/share/nginx/html/mebozo-magento.mebozo.com/blog;
try_files $uri $uri/ /blog/index.php;
index index.php index.html index.htm;
rewrite ^.*/files/(.*) /wp-includes/ms-files.php?file=$2;
rewrite ^.*/wp-admin(.*) $1wp-admin/;
}
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
root /usr/share/nginx/html/mebozo-magento.mebozo.com/blog;
rewrite ^/.*(/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(/.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$/wp-includes/ms-files.php?file=$1 last;
expires 30d;
break;
}
## These locations would be hidden by .htaccess normally
#location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
#expires 1y;
#log_not_found off;
#}
location ~ .php/ {
## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
################For Foomen Speedster###############
#rewrite ^/minify/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
# rewrite ^/skin/m/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
# location /lib/minify/ {
# allow all;
# }
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
#############gzip###########
gzip on; # use gzip compression
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_proxied any; # enable proxy for the fcgi requests
gzip_types text/plain text/css application/x-javascript text/javascript application/json;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location #handler { ## Magento uses a common front handler
rewrite / /index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
root /usr/share/nginx/html/mebozo-magento.mebozo.com;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/mebozo-magento.mebozo.com$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
I will attempt to solve this puzzle by suggesting that we clean up your nginx config file. Your */files/ rewrites look to be problematic to me.
Without knowing what your nginx.conf file looks like OR what your http {block} looks like, I will assume that it is pretty clean and that you are handling your global settings like gzip types, ssl protocols and ciphers, and additional headers, etc. there. I know that you included your gzip on in your file but sometimes duplicate that in server not realizing it is already set a layer above... if not add your gzip back in as necessary. All that said, after reading your conf file completely and I would suggest rewriting it to something like this:
(Note: the new URI level location and the #rewrites, and the removal of redundant root path definitions.)
server {
listen 80;
listen [::]:80;
## SSL CONFIGURATION (can be done here in same file)
#listen 443 ssl http2;
#listen [::]:443 ssl http2;
#ssl_certificate /etc/nginx/ssl/cert_chain.crt;
#ssl_certificate_key /etc/nginx/ssl/star_example.com.priv.key;
# domain name
server_name example.com www.example.com;
# doc root
root /usr/share/nginx/html/mebozo-magento.mebozo.com;
## Logs per vhost
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log warn;
## This can also be set in your http block and if it is, it's not needed here.
index index.php index.html index.htm;
# Adjust upload max file size settings
# This value should match your PHP.ini config settings for upload_max_filesize
client_max_body_size 50M; # allows file uploads up to 50 megabytes
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
## Main Magento location
location / {
try_files $uri $uri/ #rewrite;
}
# Your blog location
location /blog/ {
try_files $uri $uri/ #rewrite_blog;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx;
}
## These locations are protected
location ~ /(app|downloader|includes|pkginfo|var|errors/local.xml)/ {
deny all;
}
## Images
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
add_header ETag "";
}
location =/js/index.php/x.js {
rewrite ^(.*\.php)/ $1 last;
}
# rewrites
location #rewrite {
rewrite / /index.php?$args;
}
location #rewrite_blog {
rewrite /blog/ /blog/index.php?$args;
}
## Execute PHP scripts
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
## Store code with multi store/domain magento instance
#fastcgi_param MAGE_RUN_CODE $mage_code;
#fastcgi_param MAGE_RUN_TYPE $mage_type;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Banned locations (only reached if the earlier PHP entry point regexes don't match)
location ~* (\.php$|\.sh$|\.txt$|\.htaccess$|\.git|\.sample$|mage$) {
deny all;
}
}

Resources