If I have this config in sites-available in nginx for the aforementioned wordpress site, it will not display the root page (403 error), but it will display specific pages if I enter their URL manually, such as https:// subdomain.domain.tld/suchandsuch
server {
listen 80;
server_name subdomain.domain.tld;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
## Your website name goes here.
server_name subdomain.domain.tld;
## Your only path reference.
root /var/www/subdomain.domain.tld;
ssl_certificate /etc/letsencrypt/live/subdomain.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.tld/privkey.pem;
## 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_params;
fastcgi_intercept_errors on;
#fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
However if I have this config set, it will do the opposite - it will show the root page, but any other pages I click on gives a 404 error:
server {
listen 80;
server_name subdomain.domain.tld;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name subdomain.domain.tld;
root /var/www/subdomain.domain.tld;
index index.php index.html;
access_log /var/log/nginx/subdomain.domain.tld-access.log;
error_log /var/log/nginx/subdomain.domain.tld-error.log;
ssl_certificate /etc/letsencrypt/live/subdomain.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.tld/privkey.pem;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Could some part of each config be needed to be combined together to make a working config for wordpress?
I expected the wordpress files and MySQL database to function as it had on the Apache server I had it on previously.
ChatGPT did a bang-up job of helping me figure this out.
Here is the final config:
server {
listen 80;
server_name subdomain.domain.tld;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name subdomain.domain.tld;
root /var/www/subdomain.domain.tld;
index index.php index.html;
access_log /var/log/nginx/subdomain.domain.tld-access.log;
error_log /var/log/nginx/subdomain.domain.tld-error.log;
ssl_certificate /etc/letsencrypt/live/subdomain.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.tld/privkey.pem;
## 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$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
#fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Related
I have a nginx block for a wordpress installation, and a subdirectory which redirects to an app I have running. I want .php urls that go to the subdirectory to not be evaluated by fastcgi, since the app has it's own special system for evaluating them. However, php URLs still go to the fastcgi block and return a 404.
Nginx configuration:
server {
listen 0.0.0.0:443 http2 default_server;
listen [::]:443 http2 default_server;
ssl on;
ssl_certificate /etc/nginx/certificate.pem;
ssl_certificate_key /etc/nginx/certificate.key;
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;
}
root /var/www/wordpress;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /my_app {
proxy_pass http://localhost:8081/;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I want /index.php to be evaluated by fastcgi.
I want /my_app/index.php to be proxy_pass'd to my app.
I have one main wordpress app installed in this domain test.wa-essence.com,
now I want to setup a second wordpress under a subdomain test.wa-essence.com/wachampionacademy
the first wordpress in located inside /var/www/test_wa_essence
and the second wordpress is inside /var/www/wa_champion
I followed this instruction on setting the nginx https://serversforhackers.com/c/nginx-php-in-subdirectory
and here is the nginx config that I have written
server {
root /var/www/test_wa_essence;
index index.php index.html index.htm index.nginx-debian.html;
server_name test.wa-essence.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location /wachampionacademy {
alias /var/www/wa_champion;
try_files $uri $uri/ #nested;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location #nested {
rewrite /wachampionacademy/(.*)$ /wachampionacademy/index.php?/$1 last;
}
location ~ /\.ht {
deny all;
}
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;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/test.wa-essence.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/test.wa-essence.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = test.wa-essence.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name test.wa-essence.com;
return 404; # managed by Certbot
}
I managed to install both wordpress by using different nginx config.
the first app can be accessed without any problem, however, test.wa-essence.com/wachampionacademy return me 404 eventhough it appears to be at the right wordpress app.
Please tell me what I got wrong in my nginx setup. Thanks
server {
root /var/www/wa_essence;
index index.php index.html index.htm index.nginx-debian.html;
server_name test.wa-essence.com;
location /wachampionacademy/{
try_files $uri $uri/ /wachampionacademy/?$args;
}
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
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;
}
}
server {
listen 80;
server_name test.wa-essence.com;
}
that is the final nginx config that I used,
however, the most important thing that I change I believe is the wordpress siteurl and homeurl to test.wa-essence.com/wachampionacademy
I get to load root domain and categories or pages. But I can't login to /admin/ or /phpmyadmin/ but instead it downloads application/octet-stream file.
What can be done? Thanks
Here's my conf file:
server {
listen 80;
listen [::]:80; #Use this to enable IPv6
server_name localhost;
root /var/www/html/;
index index.php;
location / {
try_files $uri $uri/ #rewriteapp; }
location #rewriteapp {
# rewrite all to index.php
rewrite ^(.*)$ /index.php/$1 last; }
# Php configuration location ~ ^/(index|index_dev)\.php(/|$) {
# Php-FPM Config (Socks or Network)
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
# Security. discard all files and folders starting with a "." location ~ /\. {
deny all;
access_log off;
log_not_found off; }
# Stuffs location = /favicon.ico {
allow all;
access_log off;
log_not_found off; } location ~ /robots.txt {
allow all;
access_log off;
log_not_found off; }
# Static files location ~* ^.+\.(jpg|jpeg|gif|css|png|js|pdf|zip)$ {
expires 30d;
access_log off;
log_not_found off; }
}
i had a similar problem with
location = /images/favicons/site.webmanifest {
log_not_found off;
access_log off;
}
it download the file instead of display only
edit:
i found a solution that work, but don't know if the best...
location = /images/favicons/site.webmanifest {
default_type webmanifest;
log_not_found off;
access_log off;
}
Say I have wordpress installed and served at example.com using nginx.
I would like example.com/microsite to be served by a separate wordpress installation in another root directory.
I've included my nginx configuration which is not working. the calls to /microsite are passed to the main site's index.php and I get a 404.
server {
listen 80;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
ssl_certificate /etc/ssl/certs/Example.pem;
ssl_certificate_key /etc/ssl/private/example.key;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";
server_name www.example.com;
root /var/www/www.example.com;
index index.php;
access_log off;
client_max_body_size 15M;
gzip on;
########## BEGIN MICRO-SITES
# Mini Site Conference
location /minisite/ {
root /var/www/minisite;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
#END Mini Site Conference
######### END MICRO-SITES
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_intercept_errors on;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Was easier to just move the subdirectory inside the main wordpress file and use the following:
# Micro site
location /microsite/ {
try_files $uri $uri/ /microsite/index.php?$args;
}
It is possible to use nginx with OpenCart? I am using following nginx zone:
server {
listen 80;
listen [::]:80;
root /home/username/Development/shop.local;
index index.php;
server_name shop.local;
location /image/data {
autoindex on;
}
location /admin {
index index.php;
}
location / {
try_files $uri #opencart;
}
location #opencart {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
deny all;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
But when i visit shop.local i am getting redirecto to shop.local/install/index.php and nginx throws me an error 404. What's wrong with my config?
Try this one:
# FORCE WWW
server {
server_name site.com;
rewrite ^(.*) http://www.domain.com$1 permanent;
}
# MAIN SERVER
server {
server_name www.domain.com;
listen 80;
root /var/www/www.domain.com/public;
index index.php index.html;
location /image/data {
autoindex on;
}
location /admin {
index index.php;
}
location / {
try_files $uri #opencart;
}
location #opencart {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
deny all;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}