Currently, I'm trying to set up a Magento 2 site that will run with http://www.example.com/store/ as its home page, but it seems that Magento's router doesn't know about the subdirectory. Magento2 runs perfectly if not under a subdirectory.
Here is my settings, Wordpress is install under /var/www/wp, whereas Magento2 is intalled under /var/www/store, below is my configuration:
upstream fastcgi_backend {
server unix:/run/php/php7.0-fpm.sock;
}
server {
server_name www.example.com;
listen 80;
port_in_redirect off;
root /var/www/wp;
index index.html index.php;
error_page 419 = #magento;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /store {
root /var/www; #seems useless, it works the same even if commented.
index index.php;
autoindex off;
charset UTF-8;
#error_page 404 403 = /store/pub/errors/404.php; #if this line doesn't be commented, it will display a blank page.
location = /store {rewrite /store /store/index.php last;}
location = /store/ {rewrite /store /store/index.php last;}
location = /store/setup {rewrite /store/setup /store/setup/index.php last;}
location = /store/setup/ {rewrite /store/setup/ /store/setup/index.php last;}
location /store/setup {
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
access_log off;
expires max;
try_files $uri =404;
}
location /store/setup/index.php {return 419;}
}
location /store/pub/ {
location /store/pub/media/ {
location /store/pub/media/customer/ {deny all;}
location /store/pub/media/downloadable/ {deny all;}
location ~ /store/pub/media/theme_customization/.*\.xml$ {deny all;}
try_files $uri $uri/ /store/pub/get.php?$args;
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
expires +1y;
try_files $uri $uri/ /store/pub/get.php?$args;
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
expires off;
try_files $uri $uri/ /store/pub/get.php?$args;
}
}
location /store/pub/static/ {
expires max;
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
expires +1y;
if (!-f $request_filename) {
rewrite ^/store/pub/static/(version\d*/)?(.*)$ /store/pub/static.php?resource=$2 last;
}
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
expires off;
if (!-f $request_filename) {
rewrite ^/store/pub/static/(version\d*/)?(.*)$ /store/pub/static.php?resource=$2 last;
}
}
if (!-f $request_filename) {
rewrite ^/store/pub/static/(version\d*/)?(.*)$ /store/pub/static.php?resource=$2 last;
}
}
location ~ ^/store/pub/errors/.*\.(xml|phtml)$ {deny all;}
location /store/pub/errors/ {try_files $uri =404;}
location = /store/pub/cron.php {deny all;}
}
location ~ (index|get|static|report|404|503|11)\.php$ { return 419; }
if (-e $request_filename) {return 403;}
rewrite /store /store/index.php last;
}
# Cache static files for as long as possible
location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|cur)$ {
expires max;
log_not_found off;
access_log off;
}
# Deny public access to wp-config.php
location ~* wp-config.php {
deny all;
}
location ~ /\. {deny all;}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass fastcgi_backend;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #magento {
root /var/www;
try_files $uri =404;
include fastcgi_params;
fastcgi_pass fastcgi_backend;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
www.example.com/store will return something as attached picture;
whereas typing http://www.example.com/store/setup resulted in 404ed and address bar became http://www.example.com/store/setup/index.php/session/unlogin.
Highly appreciated if anyone could give me a clue. Many thanks.
Related
Having an issue on wordpress multisite setup with nginx, it is redirecting over and over again with the same https/url on subfolder, but main site is working. Can someone please help me? This is my configuration. Thanks in advance.
Take note: Server is EC2 instance under Application Load Balancer and Cloudfront
server {
server_name _;
listen 80 reuseport;
root /var/www/html;
index index.php;
set $upstream_endpoint ${FPM_SERVER};
set $proxy_https '';
set $csp '';
if ($http_cloudfront_forwarded_proto = 'https') {
set $proxy_https 'on';
}
if ($http_x_forwarded_proto = 'https') {
set $proxy_https 'on';
}
if ($scheme = 'https') {
set $proxy_https 'on';
}
if ($proxy_https = 'on'){
set $csp 'upgrade-insecure-requests;';
}
add_header Content-Security-Policy $csp;
location = /favicon.ico {
allow all;
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# try to serve file directly, fallback to index.php
# try_files $uri $uri/ /index.php$args;
try_files $uri $uri/ /index.php?q=$uri&$args;
proxy_set_header Host $host;
}
### ADDED CONFIG
###
# rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location /blogs {
try_files $uri $uri/ /blogs/index.php?q=$uri&$args;
}
location /faq {
try_files $uri $uri/ /faq/index.php?q=$uri&$args;
}
rewrite ^/files/(.+) /wp-includes/ms-files.php?file=$1 last;
location ^~ /files/ {
rewrite ^.*/files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}
# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
break;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS $proxy_https if_not_empty;
fastcgi_param REMOTE_ADDR $proxy_add_x_forwarded_for;
fastcgi_pass $upstream_endpoint;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
I am using nginx and my webservers htdocs folder contains several *.html files. I want to serve them even if the request doesn't contain the *.html extension.
Most answers I found here on SO apply a rewrite which will be visible in the browsers address bar. How can I achieve the same but "silently"?
Example:
www.example.com/foo => www.example.com/foo.html
If I'm not mistaken, then /opt/bitnami/apps/wordpress/conf/nginx-app.conf is the best location to apply these changes.
index index.php index.html index.htm;
if ($request_uri !~ "^/phpmyadmin.*$")
{
set $test A;
}
if ($request_uri !~ "^/bitnami.*$")
{
set $test "${test}B";
}
if (!-e $request_filename)
{
set $test "${test}C";
}
if ($test = ABC) {
rewrite ^/(.+)$ /index.php?q=$1 last;
}
# Deny access to any files with a .php extension in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
# Disable logging for not found files and access log for the favicon and robots
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
include "/opt/bitnami/apps/bitnami/banner/conf/banner-substitutions.conf";
include "/opt/bitnami/apps/bitnami/banner/conf/banner.conf";
# Deny all attempts to access hidden files such as .htaccess or .htpasswd.
location ~ /\. {
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_read_timeout 300;
fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
You could use the try_files
location / {
try_files $uri $uri/ $uri.html =404;
}
document at https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
index index.php index.html index.htm;
if ($request_uri !~ "^/phpmyadmin.*$")
{
set $test A;
}
if ($request_uri !~ "^/bitnami.*$")
{
set $test "${test}B";
}
if (!-e $request_filename)
{
set $test "${test}C";
}
if (!-e $request_filename.html)
{
set $test "${test}D";
}
if ($test = ABCD) {
rewrite ^/(.+)$ /index.php?q=$1 last;
}
location / {
try_files $uri $uri/ $uri.html =404;
}
# Deny access to any files with a .php extension in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
# Disable logging for not found files and access log for the favicon and robots
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
include "/opt/bitnami/apps/bitnami/banner/conf/banner-substitutions.conf";
include "/opt/bitnami/apps/bitnami/banner/conf/banner.conf";
# Deny all attempts to access hidden files such as .htaccess or .htpasswd.
location ~ /\. {
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_read_timeout 300;
fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
I'd like to install a software in a subdirectory of an NginX Vhost.
Currently I've got:
server {
server_name www.website.com;
root /var/www/website.com/www.website.com/;
gzip_static on;
access_log /var/log/nginx/website.com_access.log;
error_log /var/log/nginx/website.com_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
location ~ (^|/)\. {
return 403;
}
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^ /index.php;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_read_timeout 150;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri #rewrite;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Now for the subdirectory I tried:
location /mseiten/ {
try_files $uri $uri/ /mseiten/index.php?q=$request_uri;
allow all;
root /var/www/website.com/www.website.com;
}
But all I'm seeing is a 403 if I try to access /mseiten/ in my browser (it should direct me to my installation wizard). I've uncommented all rules that return 403; but with the same results.
Would it be possible to setup sth. like a clean slate for a sub directory?
So I have decided to do two kind of wrong, slightly inspired by Troy Hunt.
I would like my API to accept version specification through Accept header but also in the URL, e.g. /v1.
For now, I have made an nginx config which works with Accept header, but trying out various methods, I have not been able to get the /v1
What I want to achieve is that the URL passed to my application, does not include the version part as the role of the version is simply to point at a root directory.
map $http_accept $api_version {
default 0;
"application/vnd.it.echo.api+json; version=1" "v1";
}
server {
listen 80;
server_name api.app;
index index.html index.htm index.php;
charset utf-8;
sendfile off;
rewrite_log on;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location / {
if ($api_version = 0) {
return 307 https://echo.it;
}
try_files $uri $uri/ #api;
}
location /v1 {
set $api_version "v1";
rewrite ^/.+/(.+)$ /$1 last;
}
location #api {
root /home/vagrant/api/$api_version/public/;
error_log /var/log/nginx/api.$api_version.app-error.log error;
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
root /home/vagrant/api/$api_version/public/;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
The problem appears to be with the rewrite rule in the /v1 location directive not taking effect.
I have a debian machine on which I installed nginx for my web server worker process. But we just need to change a little for wordpress in default nginx configuration which is located /etc/nginx/sites-enabled/ !
/etc/nginx/sites-enabled/wordpress.com
/etc/nginx/sites-enabled/drupal.com
Can I know sample nginx configurations for those two websites under single debian machine.
You can run multiple sites using nginx, analogous to an apache vhost.
In /etc/nginx/sites-enabled, you can add two vhosts
wordpress.example.com
server {
listen 80;
server_name wordpress.example.com;
root /var/www/wordpress;
# if ($http_host != "www.example.com") {
# rewrite ^ http://www.example.com$request_uri permanent;
# }
index index.php index.html;
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).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
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;
}
}
and
drupal.example.com
server {
listen 80;
server_name drupal.example.com;
root /var/www/drupal;
# if ($http_host != "www.example.com") {
# rewrite ^ http://www.example.com$request_uri permanent;
# }
index index.php index.html;
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).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
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:9001;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}