I have a nginx conf for my wordpress site. it's too much rewrite.
I want to replace the rewrite to try_file and reduce the conf file.
This is my nginx conf:
server
{
listen 80;
server_name domain.com www.domain.com;
index index.php;
root /home/domain.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ^~ /topics/ {
try_files $uri $uri/ /index.php;
rewrite ^/topics/(.*).html$ $scheme://$host/topics/$1/ permanent;
rewrite ^/topics/(.*).html/(.*)$ $scheme://$host/topics/$1/$2 permanent;
rewrite ^/topics/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/$ $scheme://$host/topics/$4/ permanent;
rewrite ^/topics/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/(.*)$ $scheme://$host/topics/$4/$5 permanent;
rewrite ^/topics/tag/(.*)$ $scheme://$host/tag/$1 permanent;
rewrite ^/topics/category/(.*)$ $scheme://$host/category/$1 permanent;
}
location ~ ^/topics/.+\.php$ {
set_real_ip_from 127.0.0.1;
real_ip_header X-Real-IP;
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
rewrite ^/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/$ $scheme://$host/topics/$4/ permanent;
location ~ .*\.php(\/.*)*$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
set_real_ip_from 127.0.0.1;
real_ip_header X-Real-IP;
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
access_log off;
}
How can I reduce and replace it? Thank you.
Related
I have an nginx configuration like this:
server{
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/all-my-projects;
location / {
try_files $uri $uri/ =404;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
}
location ~ \.php$ {
fastcgi_pass $php_fastcgi_pass;
fastcgi_index /index.php;
include fastcgi_params;
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $fastcgi_script_name =404;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# These are the locations I'm havin' trouble
location /project {
try_files $uri #store-deprecated;
}
location #project {
rewrite /project/(.*)$ /project/public/index.php?_url=/$1 last;
}
}
but when I go to localhost/project the images on the browser doesn't load on any view.
I know I rewrote when I go to the project folder, it redirects to project/public/index.php and run the site.
I put the images in the html like this:
<img src="img/image.png">
How can I rewrite the public/img/ folder to access all the images by typing localhost/project/img/image.png?
Same thing with a css and js folders and a favicon.ico
server {
rewrite ~ ^(/img/.*) /public$1 break;
}
I've got an nginx on my server and I am trying to get it to open the file '/config/www/pp1/index.php' for address https://example.com/pp1 and '/config/www/interpreter/index.html' for https://example.com/interpreter. Furthermore all things like https://example.com/interpreter/res/docs should fire up '/config/www/interpreter/res/docs.html'. I have made many attempts. Currently my default config file in /site-confs looks like this:
server {
listen 80;
listen 443 ssl http2;
server_name kni.mini.pw.edu.pl;
# Path for SSL config/key/certificate
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
location / {
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_pass http://kni_website;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location /pp1 {
root /config/www;
index index.html index.htm index.php;
try_files $uri $uri/ index.php $uri/index.php /config/www/pp1/index.php index.php; #/index.php?$args =404;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
location /interpreter {
if ($request_uri ~* "([^/]*$)" ) {
set $last_path_component $1;
}
root /config/www;
index index.html index.htm index.php $last_path_component.html;
try_files /interpreter/res/$last_path_component.html $uri.html $uri.html $uri $uri/ /index.html index.php /$uri.html /index.php?$args =404;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
}
server {
listen 80 default_server;
listen 443 ssl;
root /config/www;
index index.html index.htm index.php;
server_name _;
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
client_max_body_size 0;
location / {
try_files $uri $uri/ /index.html /index.php?$args =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
Quite frankly, I thought I knew what I was doing, however right now I am pretty certain that the location block location /interpreter is not being checked at all and the insides of location /pp1 are causing some crazy mumbo-jumbo.
Please help a newbie in need!
The main problem is that try_files will process its file elements within the current context, so you cannot handle your .html and .php URIs in the same statement. See this document for details.
One solution is to use a named location to split the try_files statement into two. First to test $uri, $uri.html and $uri/index.html, then a second to test $uri.php and $uri/index.php.
For example:
root /path/to/root;
location /foo {
try_files $uri $uri.html $uri/index.html #php;
location ~* ^(.*)\.php$ { return 301 $1; }
}
location #php {
try_files $uri.php $uri/index.php =404;
fastcgi_pass ...;
...
}
The location ~* ^(.*)\.php$ block is added to handle URIs that end with .php correctly. The simplest solution is to redirect them to a URI with the .php removed.
I am havign some problems with product url on this web http://www.hobby-italia.it
Nginx 1.10.1
Prestashop 1.6
This is my /etc/nginx/site-available/www.hobby-italia.it conf file
server {
server_name www.hobby-italia.it;
listen 80;
port_in_redirect off;
access_log /var/log/nginx/hobby-italia.access.log;
error_log /var/log/nginx/hobby-italia.error.log;
root /var/www/www.hobby-italia.it/;
index index.php index.html;
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# 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;
}
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
As you can see going to one product resturn 404 not found error
http://www.hobby-italia.it/trapani-a-filo/9-trapano-a-percussione-martello-demolitore-tassellatore-sds-ribimex-1500w-3700194404178.html
I have this directive in my nginx virtual host file:
try_files $uri $uri/ /profile.php?nickname=$uri&$args;
It works ok, but I want to rewrite only 0-9a-z- characters so that users have nice profile addresses, for example:
http://www/mydomain.com/john
But nginx rewrites everything. For example, if I enter address:
http://www.mydomain.com/non-existing-holder/nonexisting-filename.html
instead of returning 404 error it rewrites everything.
Is there any way how to rewrite only 0-9-a-z- characters?
server {
listen 80;
server_name www.mydomain.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/mydomain;
error_page 404 /4044.html;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /profile.php?nickname=$uri&$args;
rewrite ^/forum/([0-9-]+)/([0-9a-z-]+).html$ /forum.php?tid=$1&title=$2 last;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
You can try the following configuration.
Request for /testUSER would return a 404
Request for /forum/100/something.html would go to /forum.php?tid=100&title=something
Request for /nickname would go to /profile.php?nickname=nickname
.
location / {
index index.html index.htm index.php;
rewrite ^/([0-9a-z-]+)/?$ /profile.php?nickname=$1&$args last;
try_files $uri $uri/ =404;
}
location /forum {
rewrite ^/forum/([0-9-]+)/([0-9a-z-]+).html$ /forum.php?tid=$1&title=$2 last;
alias /var/www/mydomain/forum;
}
I'm using Wordpress as root of my website and Invision Power Boards as forum.
http://localhost -> Wordpress
http://localhost/forum -> IPB
I have removed "index.php" from Wordpress URLs successfully with Nginx-rewrite however when I try to use SEO Friendly URLs on IPB, nginx simply returns to Wordpress' 404 page.
My configuration is like this:
#This removes "index.php" from Wordpress URLs
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Then I follow this link to modify my nginx conf file in order to be able to use SEO friendly URLs of IPB: http://www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-and-nginx/
#This part is to be able to use IPB SEO
location /forum/ {
index index.php;
try_files $uri $uri/ /forum/index.php?$uri&$args;
rewrite ^ /index.php? last;
}
When I click a link on my forum (for example: http://localhost/forum/index.php/forum/51-sport/) nginx simply redirects me to (http://localhost/forum/forum/51-sport/) which displays Wordpress 404 error page.
I have very little knowledge about regex so any help would be appreciated.
This is my whole conf file after modifications, little messy I accept that.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/user_name/public_html;
access_log /var/log/nginx/a/access.log;
error_log /var/log/nginx/a/error.log
server_name localhost;
server_tokens off;
location / {
try_files $uri $uri/ #wordpress;
}
location #wordpress {
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME /index.php;
}
location /forum {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
location /forum/ {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
#location / {
#index index.php index.html index.htm;
#try_files $uri $uri/ /index.php?q=$uri&$args;
#}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/)(/.*)$;
}
# Add trailing slash to */wp-admin and */forum requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9001
#location ~ \.php$ {
# fastcgi_split_path_info ^(/)(/.*)$;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
# fastcgi_param PATH_INFO $fastcgi_script_name;
# include /etc/nginx/fastcgi_params;
#REMOVE THIS
#fastcgi_read_timeout 60000;
#fastcgi_send_timeout 6000;
#}
}
Since the last post, I have played with IPB's SEO configurations and I managed to remove "index.php" from URLs. It doesn't effect the result of course. But it seems that location / decides what to do and therefore link is being considered as a Wordpress permalink.
EDIT - Solution
# Upstream to abstract backend connection(s) for php
upstream php {
# server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9001;
}
server {
## Your website name goes here.
server_name localhost;
## Your only path reference.
root /home/username/public_html;
## 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
try_files $uri $uri/ /index.php;
}
location /forum {
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? break;
}
location ~ ^/forum/index.php {
if ($args != "") {
rewrite ^ http://www.google.com/ permanent;
}
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? last;
}
location /forum/admin/ {
try_files $uri $uri/ /forum/admin/index.php;
rewrite ^ /forum/admin/index.php? last;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include /etc/nginx/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
I'm too using wordpress and ipb on nginx, now configuring, i added wordpress permalink to ipb config and turn on seo Rewrite URLs, Force Friendly URLs
location / {
try_files $uri $uri/ /index.php?$args;
}
it worked for me