How to redirect NGINX - php

I'm trying to redirect from
https://support.example.com.br/
for
https://support.example.com.br/?SSO=1
how do i do this through nginx?
my code is like this:
server {
listen 8080;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location / {
rewrite ^/$ /?SSO=1$1 redirect;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SERVER_NAME $host;
}
location ~ /\.ht {
deny all;
}
location ~ php-errors\.log$ {
deny all;
}
}

server {
...
rewrite ^/$ /?SSO=1 permanent;
}

Please, do not ask second, third and so on questions about the same problem. Use the comments system to clarify your problems with the particular answer. To prevent a redirect when the SSO query argument already specified in request, try this:
server {
... # global server block directives
if ($arg_SSO = '') {
rewrite ^/$ /?SSO=1 permanent;
}
... # your locations goes here
}

Related

nginx 404 not found in subfolder

I installed a PHP script in /files/ subfolder on Nginx but when I access the page, it's not working. Just showing 404 not found. I also tried changing the root directly to the subfolder but still not working, like this: root /home/smart/web/example.com/public_html/files;
Also, I added this in the config but not working.
location ^~ /files {
if (!-e $request_filename) { rewrite ^/(.*) /files/index.php?_page_url=$1 last; }
location ~ \.php$ {
if (!-e $request_filename) { rewrite ^/(.*) /files/index.php?_page_url=$1 last; }
fastcgi_pass unix:/run/php/php8.0-fpm-example.com.sock;
index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location /files/files/ {
internal;
}
# these locations would be hidden by .htaccess normally
location /files/logs/ {
deny all;
}
}
Here is my full nginx config:
#=========================================================================#
# Default Web Domain Template #
# DO NOT MODIFY THIS FILE! CHANGES WILL BE LOST WHEN REBUILDING DOMAINS #
# https://docs.hestiacp.com/admin_docs/web.html#how-do-web-templates-work #
#=========================================================================#
server
{
listen myip:443 ssl http2;
server_name example.com;
root /home/smart/web/example.com/public_html;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/example.com.log combined;
access_log /var/log/nginx/domains/example.com.bytes bytes;
error_log /var/log/nginx/domains/example.com.error.log error;
client_max_body_size 5G;
ssl_certificate /home/smart/conf/web/example.com/ssl/example.com.pem;
ssl_certificate_key /home/smart/conf/web/example.com/ssl/example.com.key;
ssl_stapling on;
ssl_stapling_verify on;
include /home/smart/conf/web/example.com/nginx.hsts.conf*;
location /
{
location ~* ^.+\.(jpeg|jpg|png|webp|gif|bmp|ico|svg|css|js)$
{
expires max;
fastcgi_hide_header "Set-Cookie";
}
location ~ [^/]\.php(/|$)
{
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name)
{
return 404;
}
fastcgi_pass unix:/run/php/php8.0-fpm-example.com.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
include /home/smart/conf/web/example.com/nginx.fastcgi_cache.conf*;
}
}
location ^~ /files {
if (!-e $request_filename) { rewrite ^/(.*) /files/index.php?_page_url=$1 last; }
location ~ \.php$ {
if (!-e $request_filename) { rewrite ^/(.*) /files/index.php?_page_url=$1 last; }
fastcgi_pass unix:/run/php/php8.0-fpm-example.com.sock;
index
index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location /files/files/ {
internal;
}
# these locations would be hidden by .htaccess normally
location /files/logs/ {
deny all;
}
}
location /error/
{
alias /home/smart/web/example.com/document_errors/;
}
location ~ /\.(?!well-known\/)
{
deny all;
return 404;
}
location /vstats/
{
alias /home/smart/web/example.com/stats/;
include /home/smart/web/example.com/stats/auth.conf*;
}
include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /home/smart/conf/web/example.com/nginx.conf_*;
}

nginx 403 forbidden error + mac + laravel

Here is my nginx's content.
My current access url is http://localhost/lampi/, and I am receiving response 403 forbidden.
My server's documentroot is /Library/WebServer/Documents/.
When I access http://localhost/, it shows ok. I can also see the index.html page's content.
I don't know what the matter is. I have checked top 10 pages in stackoverflow.
server {
server_name localhost;
access_log /var/log/nginx/nginx.host.access.log main;
root /Library/WebServer/Documents/;
location / {
#root html;
index index.html index.htm index.php;
}
location /lampi {
#autoindex on;
if (!-e $request_filename){
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
}
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Library/WebServer/Documents/lampi/$fastcgi_script_name;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
#location /images/ {
# root /usr/local/var/www;
#}
}
There are three potential problems with your configuration file.
The use of if (!-e $request_filename) is causing problems because it checks for the existence of directories, and you should probably be using try_files anyway (see this document for details):
location /lampi {
try_files $uri $uri/ #lampi;
}
location #lampi {
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
The value of SCRIPT_FILENAME adds an extra /lampi into the pathname. Use either of (both evaluate to the same value in this case):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
For example:
location ~ \.php$ {
try_files $uri =404;
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
The location ~ [^/]\.php(/|$) block is confusing. Normally it is adequate to use either location ~ \.php$ or something like location ~ [^/]\.php(/|$) depending on whether or not your application uses PATH_INFO. Delete the block you are not using. See this document for details.

Redirecting to controller/action

I want to be able to convert the following link:
http://domain.com/foo/bar
into http://domain.com/index.php?controller=foo&action=bar
using php5-fpm. I also want to be able to access static files inside www/ folder. How do I do that? This is what I have so far:
server {
charset utf-8;
client_max_body_size 8M;
listen 80; ## listen for ipv4
server_name domain.com;
root /var/www/domain.com/www;
index index.php;
access_log /var/log/nginx/domain.com.access.log;
error_log /var/log/nginx/domain.com.error.log;
location / {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
But it gives me blank page and no get parameters. How should I do this?
The try_files directive is useful when serving static files, if they exist, and rewriting the URI if they do not. See this document for details.
There are a number of ways to achieve this, for example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}

Redirect 404 error to index file in subdirectory

I want to redirect 404 errors on http://test.com/folder1/ to http://test.php/folder1/index.php .
Server is running with nginx and php-fpm. Current virtual host is given below.
server {
listen x.x.x.x:80;
server_name test.com www.test.com;
access_log off;
#error_log /dev/null;
error_log /var/log/nginx/error.log;
root /home/test/public_html;
location / {
index index.php;
location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
expires 7d;
}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm/test.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ /\.ht {
deny all;
}
}
Currently 404 errors are redirecting to http://test.com/index.php . 404 errors should be redirected to index.php in every subfolder.
thanks

Rewrite NGINX URL Location?

I want to use Baikal on my server together with NGINX. The index.php of baikal is in /baikal/html
The request for the following configuration works with this url:
https://www.mydomain.com:8001/baikal/html
How can I change the NGINX configuration that
https://www.mydomain.com:8001/baikal
gets redirected to https://www.mydomain.com:8001/baikal/html?
Here is my NGINX configuration:
server {
listen 8001;
ssl on; # <-------------------------------------------- SSL
ssl_certificate /etc/nginx/ssl/seahub.crt; # <--------- SSL
ssl_certificate_key /etc/nginx/ssl/seahub.key; # <----- SSL
server_name confile.no-ip.biz; #.tld; # <----------------- CHANGE THIS
root /usr/share/nginx/www;
index index.html index.htm index.php;
rewrite ^/.well-known/caldav /cal.php redirect;
rewrite ^/.well-known/carddav /card.php redirect;
location / {
location ~ ^(.+\.php)(.*) {
try_files $fastcgi_script_name =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
include fastcgi_params;
}
charset utf-8;
location ~ /(\.ht|Core|Specific) {
deny all;
return 404;
}
}
}
First of all, you should not have a location inside of another location. Make sure they're all seperate.
Next, to actually create the rewrite:
location = /baikal {
return 301 /baikal/html
}
should do the trick.

Categories