how to enable url rewriting in nginx on windows pc - .htaccess

I am using winginx on windows 8.1 pro. I am new to using it. I want to enable url rewriting for nginx on windows so that link like this"http://mysite.dev/portfolio/latest/business-card-design/" works fine. I want to work with wordpress and laravel specially. I found how to convert .htaccess file to nginx but I do not know where to put them. I hope you expert people understand my problem. sorry for my English. Please help me. Thanks.

I downloaded Winginx and installed Wordpress 4.0.1 and this is my configuration :
server {
listen 127.0.0.1:80;
server_name example.com www.example.com;
root home/example.com/public_html;
index index.php index.html;
log_not_found off;
access_log logs/example.com-access.log;
charset utf-8;
location ~ /\. { deny all; }
location = /favicon.ico { }
location = /robots.txt { }
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
}
With this configuration, generated by Winginx, my site works fine and the only part that I have added is the URL rewrite part :
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
Of course you have to select your permalinks settings from Settings > Permalinks to get what you want as URLs.

Related

Nginx rewrite rule dynamic sub-folders and path

I am trying to rewrite a URL pattern to sub-folder.
location ~^ /test/article/(.*) {
root /var/www/example.com/test;
index index.html;
try_files $uri $uri/ /test/index.html;
}
When a user requests http://www.example.com/test/article/article-name,
I need to rewrite the URL to http://www.example.com/test/, but URL should stay the same, it should display as http://www.example.com/test/article/article-name in the browser.
"article-name" could be any thing with dashes. But "article" is the hard coded path, not a real folder.
My server is currently hosting Wordress in root directory of Nginx server which is /var/www/example.com/ . I tried different things with Nginx config file and not working yet. Currently, when I typed in http://www.example.com/test/article/article-name , Wordpress will show 404 error.
Any help would be appreciated.
Thank you.
Updates:::::::
Here is my latest config file under sites-available folder. It's showing Wordpress's home page when I tried the URL.
server {
listen 8080 default_server;
listen [::]:8080 default_server;
port_in_redirect off;
set_real_ip_from 127.0.0.1/32;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
root /var/www/example.com;
index index.php index.html;
server_name example.com www.example.com;
rewrite ^/test/article/(.*) /test/;
location /test/ {
root /var/www/example.com/test/;
index index.html;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
Another update:
I am now testing with following config and it doesn't show Wordpress's 404 anymore. But it actually redirects to /test folder. I need to keep the url with "/test/article/article-name" in it.
rewrite ^/test/article/(.*) /test/;
location /test/ {
index index.html;
}
rewrite ^/test/article/(.*) /test;
location /test {
root /var/www/example.com/;
}
# create file `test` in `/var/www/example.com/`
Choice II
rewrite ^/test/article/(.*) /test/;
location /test/ {
root /var/www/example.com/test/;
index index.html;
}
# create directory `test` in `/var/www/example.com/`
# and create file `index.html` in `/var/www/example.com/test/`

nginx simple deny rule not enforced

I'm kind of ashame to bother you with a question so simple but I cannot figure out why the configuration of nginx is not enforcing a simple deny rule for a subfolder and really hope you can push me to the obvious mistake.
So, the setup: I got an nginx webserver running, /var/www is root directory and there are some subfolders. SSL is enforced and as I am using baikal CalDAV/CardDAV the settings for fastcgi origin from the corresponding documentation. Here is the currently working config file:
server {
listen 443 ssl;
root /var/www;
index index.html index.htm index.php;
server_name mydomain.org;
ssl_certificate /path/to/nginx.crt;
ssl_certificate_key /path/to/nginx.key;
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~ ^(.+\.php)(.*)$ {
try_files $fastcgi_script_name =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include /etc/nginx/fastcgi_params;
}
rewrite ^/.well-known/caldav /baikal/cal.php redirect;
rewrite ^/.well-known/carddav /baikal/card.php redirect;
charset utf-8;
location ~ /(\.ht|Core|Specific) {
deny all;
return 404;
}
}
Now, I simply want to restrict access to a subsubfolder called /my/data/ which I first added in the deny block like this:
location ~ /(\.ht|Core|Specific|my/data/) {
deny all;
return 404;
}
But this did not work out, so I defined an own location like:
location ~ /my/data {
deny all;
return 404;
}
Tried with and without trailing slash, with and without ~ like in /doc/ as well as putting the deny location block before any other one and now I am at a loss. Access to /my/data and all subfolders and files is still granted. Can anyone help me out?
Thanks for reading!
Location order is important if you are using locations with regular expressions, as locations given by regular expressions are checked in order and first matched wins. That is, a configuration like this:
location ~ \.php$ { fastcgi_pass ... }
location ~ /my/data { deny all; }
will always allow access to any .php files, even matching /my/data. To fix this, you have to maintain proper order of locations, i.e., keep location ~ /my/data first:
location ~ /my/data { deny all; }
location ~ \.php$ { fastcgi_pass ... }
Or, better yet, use prefix location with the ^~ modifier instead - in this case order will not be important. This also ensures that proper prefix matching will be used, and regular expressions won't be checked at all. E.g.:
location ~ \.php$ { fastcgi_pass ... }
location ^~ /my/data { deny all; }
See http://nginx.org/r/location for more details about location matching in nginx.
Note well that return 404 in your configuration is redundant if you use deny all, or vice versa. Just use one of the directives.
It's also important to note that when testing, you shouldn't rely on your browser. Testing with browser often leads to confusing results due to caching. It's better idea to use, e.g., curl.

Nginx Rewrite and FastCGI - Php files get downloaded

I've added this block of directives to my Nginx installation
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass unix:/var/run/php5-fpm.sock;
# With php5-fpm:
include fastcgi.conf;
fastcgi_index index.php;
}
If i contact http://myserverip/script.php everything goes fine.
I need to use the rewrite engine to rewrite some URLs, after this block of directives i've added many other blocks like this:
location = /sentmessages {
rewrite ^(.*)$ /sent_messages.php break;
}
(i've used winginx converter for .htaccess rules)
If i contact http://myserverip/sentmessages rewrite goes well, but the PHP script gets downloaded instead of being passed to FastCGI.
I don't know how to fix this(tried to change the order of the directives without success.)
How to fix? thanks.
After searching Stackoverflow, solution was to use the "last" at the end of the rewrite rule.
location = /sentmessages {
rewrite ^(.*)$ /sent_messages.php last;
}

design files are not loading for magento in nginx server

My client moved to new server which nginx & percona server. Problem is the forward slash is being removed from end of the url. as you can see on the picture http://imgur.com/8OAUQZb
therefor the design files such as js, css files are not loading or something it gives magento 404 not found page. it happens randomly.
on the database web/unsecure/base_url and web/secure/base_url are set with forward slash http://78.137.115.47.srvlist.ukfast.net/
I'm assuming something wrong with nginx file. The rewrite rules might be wrong. I have tried every possible way that I found on this website and on google but nothing work. It might be something else. Could you please help?
This is nginx conf file for the domain
# Uncomment the server definition below should you wish to
# redirect from punkyfish.com to www.punkyfish.com
#server {
# listen 192.168.92.247;
# server_name punkyfish.com;
# #rewrite / $scheme://www.$host$request_uri permanent;
#}
#
# Change this backend name (and the socket pointer)
# as additional virtual hosts are added. This should
# point to the spawn-fcgi wrapper running as the
# appropriate user.
#
upstream punkyfishcombackend {
server unix:/var/run/php-fcgi-punkyfishcom.sock;
}
server {
listen 192.168.92.247:80;
server_name punkyfish.com;
root /var/www/vhosts/punkyfish.com/htdocs;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
location /app/ { deny all; }
location /includes/ { deny all; }
location /lib/ { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/ { deny all; }
location /report/config.xml { deny all; }
location /var/ { deny all; }
location /. {
return 404;
}
location #handler {
rewrite / /index.php;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
include "ssl_offloading.inc";
location ~ .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass punkyfishcombackend;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_param MAGE_RUN_CODE default;
# fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
This was due to nginx was caching everything. regardless of my changes on db and files wasn't reflecting. I cleaned the cache from ngnix cache folder and problem solved.
Thanks.

I can access my subdomain URL paths on both my domain and subdomain?

I have a DigitalOcean VPS running nginx which has two websites on it. The two sites on it are: www.ingledow.co.uk and blog.ingledow.co.uk.
My main (www.) domain is predominantly a static site, but my blog (blog.) subdomain runs on Ghost.
Everything works perfectly apart from that I can access my blog from both www. and blog.. For example, here is a blog post at http://blog.ingledow.co.uk/puma-social-club/, but the same blog post can be seen from http://www.ingledow.co.uk/puma-social-club/.
Another point to note is that if you try to go to http://ingledow.co.uk/puma-social-club/ without the www. or blog. it 404s.
The problem lies in having two sites on the same VPS, but not sure if there's a problem with my nginx configs or whether it's problems with my DNS, or both?
The nginx config files are in /sites-available/ and symlinked to /sites-enabled/
I need to get this fixed because it is causing issues with Google search results and SEO.
Here's my DNS:
blog.ingledow.co.uk.conf
# blog.ingledow.co.uk running on Ghost
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/ghost;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name blog.ingledow.co.uk;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:2369;
client_max_body_size 10m;
break;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /phpmyadmin { index index.php; }
}
ingledow.co.uk.conf
# ingledow.co.uk.conf
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/ingledow.co.uk/public_html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name ingledow.co.uk;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /phpmyadmin { index index.php; }
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Try adding www. to sever_name ingledow.co.uk; in the ingledow.co.uk server block. e.g. :
server_name www.ingledow.co.uk ingledow.co.uk;
If you don't want to site to be accessed without the www. subdomain prefix you should remove it from the server_name.
Another way to do it is to have the server block as is for the blog, and just use a catch all server block for the main static site.

Resources