I'm trying to change from plain http://sitename/?p=123 to http://sitename/postname with rewrite rule but it gives error 404 Not found. This is my nginx server block code:
server {
listen 80;
listen 443 ssl;
root /var/www/sitename/html;
index index.php index.html index.htm;
server_name sitename www.sitename;
client_max_body_size 10M;
# Certificates handled by CertBot
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
server_name sitename www.sitename;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ /index.php$is_args$args;
# rewrite ^ http://$server_name$request_uri permanent;
rewrite ^/(.*)$ http://$server_name/$1 permanent;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# root /data/www;
# index index.html index.htm;
}
}
I want to use rewrite so I redirect https of front-end to http and wp-admin and wp-login as https which is handled by Wordpress.
How can I fix it?
Created a separate similar server block with 443 listen. Included the cert lines and removed the location from 443 block.
Related
I'm using nginx to server a php application and I want to rewrite some urls internally without changing them in the client's browser. So that when the client visits api.domain.com/v1.0/products it serves the content of api.domain.com/api/v1.0/products.
Here is my current configuration for nginx
server {
listen 80; listen [::]:80;
server_name api.domain.com www.api.domain.com;
location / {
return 301 https://admin.domain.com$request_uri;
}
}
server {
listen 443 ssl http2;
server_name api.domain.com www.api.domain.com;
index index.php index.html;
root /var/www/app/public;
rewrite ^/(v\d+.*\d*)/(\w+) /api/$1/$2 redirect;
location / {
client_max_body_size 5M;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
client_max_body_size 5M;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/public/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PHP_VALUE "upload_max_filesize=5M \n post_max_size=5M";
}
}
currently this one works but it changes the url in the client browser since rewrite is used with redirect flag. but if I change the flag to last or remove it, it just returns 404 error. Is there a way to accomplish this without sending the clients to a new url?
I have a config that handles different "websites" a main root handles the requests for the main url and I specify a specific path for it, in another location /mailtracker I handle a different app and point it to a different directory. My problem is php-fpm is not finding this second location.
Basically I want to have several locations pointing to different roots.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/sites/dorero/;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /mailtracker {
alias /var/www/sites/mailtracker/web/;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 192.168.10.3:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location ~ ^/(index|app|app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
fastcgi_pass 192.168.10.3:9000;
# # With php5-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I tryied specifying instead of $document_root the path to the alias /var/www/sites/mailtracker/web/ and it didn't work.
Output from the php-fpm to this config says:
192.168.10.4 - 06/Feb/2018:19:04:49 +0000 "GET /mailtracker/index.php" 404
Put location /mailtracker above location / because nginx matched route /mailtracker for / before checking /mailtracker
You can remove location ~ \.php$ inside location /mailtracker
Add try_files $uri $uri/ =404; to location /mailtracker
edit:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/sites;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location /mailtracker {
alias /var/www/sites/mailtracker/web;
set $subfolder "mailtracker/web";
try_files $uri #rewriteapp;
}
location / {
root /var/www/sites/dorero;
set $subfolder "dorero";
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ ^/(index|app|app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
fastcgi_pass 192.168.10.3:9000;
# # With php5-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$subfolder/$fastcgi_script_name;
}
}
I have set the two Sub domain for front end and back end code. I want to redirect the new domain name if user hit the old domain. But I am getting error- "414 Request-URI Too Large". I am using Nginx server for back end code.
server {
root /var/www/html/frontendCode/dist;
index index.php index.html index.htm;
server_name dev.olddomain.com;
location / {
try_files $uri $uri/ /index.html;
}
rewrite ^ $scheme://dev.newdomain.com$request_uri permanent;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name devapi.olddomain.com;
root /var/www/html/apiBackendCode/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
rewrite ^ $scheme://devapi.newdomain.com$request_uri permanent;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
}
}
you can redirect using this:
server {
listen 80;
server_name dev.domain1.com;
rewrite ^/(.*) https://dev.domain2.com/$1 permanent;
}
Try this
server {
server_name .mydomain.com;
rewrite ^ http://www.adifferentdomain.com$request_uri? permanent;
}
you can try redirecting like that.
return 301 http://dev.newdomain.com$request_uri;
Im using nginx with a very simple configuration, it works for all php sites in the subdirs of /usr/share/nginx/www/.
But now id like to make a new project in a subdirectory with rewrite rules.
So i decide to make a .conf for this beside the default.
But the rewriting is not working cause of the error "rewrite or internal redirection cycle".
default
server {
listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# 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;
allow ::1;
deny all;
}
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 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_intercept_errors on;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
}
newproject.conf
# nginx configuration
autoindex off;
location /newproject/ {
if (!-e $request_filename) {
rewrite ^/newproject/(.+)$ /newproject/index.php?url=$1 break;
}
}
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
This part about fall back to 404 is wrong. Probably you have missed =404 here, which resulted in redirection cycle (it redirects to /index.html again and again).
Please note from the documentation:
If none of the files were found, an internal redirect to the uri specified in the last parameter is made.
I am trying to set up an NGINx server to work with Phalcon PHP Framework.
So far I've been looking for help on the internet but I could not find anything...
My conf file is:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# 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;
}
What should add to it in order to make Phalcon work?
Thank you.
I faced the same problem and finally got this working here is my configuration file using
Windows 7 and PHP 5.3 in Fast CGI mode.
server {
listen 80;
server_name localhost;
set $root_path 'C:/devtools/phalcon/test/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
try_files $uri =404;
fastcgi_index /index.php;
fastcgi_pass 127.0.0.1:9123; #default is 9000, i am using 9123
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}