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;
}
Related
I have a react website which I am serving using NGINX. I wanted to create a blog for the same. So I tried to use wordpress in a sub-directory.
`
server {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name domain.com;
location / {
try_files $uri $uri/ =404;
}
location ^~ /blog {
client_max_body_size 10m;
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^/wordpress(/.+\.php)(.*)$;
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
#ssl certificates here
}
`
After hours of reading docs, blogs and stack I got my homepage set up. However all my pages on the blog are returning 404. I am attaching my nginx config.
My directory structure is
/var/www/html/ : root folder for my react website
/var/www/html/blog : root folder for my wordpress ( no /wordpress subfolder present)
Add this lines
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/blog
}
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 have the following server block and I'm trying to do a 301 redirect so www.realestatelicensebystate.com goes to http://realestatelicensebystate.com for SEO purposes. Problem is, after I put the line in there, I'm getting the generic "Welcome to nginx" screen. Here is the code:
server {
listen 80;
server_name www.realestatelicensebystate.com;
rewrite ^/(.*)$ http://realestatelicensebystate.com/$1 permanent;
access_log /srv/www/realestatelicensebystate.com/logs/access.log;
error_log /srv/www/realestatelicensebystate.com/logs/error.log;
location / {
root /srv/www/realestatelicensebystate.com/public_html;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/realestatelicensebystate.com/public_html$fastcgi_script_name;
}
}
Anything stand out or is there anything I should be doing better? I'm brand new to nginx and learning.
The reasons why you are getting default nginx page is here:
server_name www.realestatelicensebystate.com;
rewrite ^/(.*)$ http://realestatelicensebystate.com/$1 permanent;
Your server is listening for name www.realestatelicensebystate.com and you are redirecting to realestatelicensebystate.com.
You need to create either another vhost config or new server block for your new name.
server {
listen 80;
server_name www.realestatelicensebystate.com;
rewrite ^ http://realestatelicensebystate.com$request_uri? permanent;
}
server {
listen 80;
server_name realestatelicensebystate.com;
access_log /srv/www/realestatelicensebystate.com/logs/access.log;
error_log /srv/www/realestatelicensebystate.com/logs/error.log;
location / {
root /srv/www/realestatelicensebystate.com/public_html;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/realestatelicensebystate.com/public_html$fastcgi_script_name;
}
}
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.
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