Rewrite NGINX URL Location? - php

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.

Related

Nginx rewrite not working without redirect flag

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?

Nginx Config 2 Web Applications on one Server

Good Morning guys,
i am trying to create an working Nginx Config.
I have two web applications:
/app/web
/app/api
My URL should look like this:
10.X.X.XX => /app/web
10.X.X.XX/api => /app/api
My current config:
server {
listen 80 default_server;
index index.php index.html index.htm;
root /app/web;
location /api {
root /app/api;
}
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
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;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M;
}
Any suggestions?
You can host multiple site if you follow below configuration. This is a working code. You can modify it according to your need
server {
# Listing port and host address
# If 443, make sure to include ssl configuration for the same.
listen 80;
listen [::]:80;
server_name 192.168.0.132;
# Default index pages
index index.php;
# Root for / shipment
root /var/www/msdsl/shipment/public;
# Handle main root / shipment
location / {
#deny all;
try_files $uri $uri/ /index.php?$args;
}
# Handle restora project, just replicate this section for further projects app3, app4
# by just replacing restora with appropriate tag(project1,/project2/project 3)
location /restora {
# Root for this project
root /var/www/msdsl/restora/public;
# Rewrite $uri=/restora/xyz back to just $uri=/xyz
rewrite ^/restora/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
location /tposreport {
# Root for this project
root /var/www/msdsl/tposreport/public;
# Rewrite $uri=/tposreport/xyz back to just $uri=/xyz
rewrite ^/tposreport/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
# Handle all locations *.php files (which will always be just /index.php)
# via factcgi PHP-FPM unix socket
location ~ \.php$ {
# At this point, $uri is /index.php, $args=any GET ?key=value and $request_uri = /restora/xyz.
# But we don't want to pass /restora/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below.
# This allows laravel to see /restora/xyz as just /xyz in its router.
# So laravel route('/xyz') responds to /restora/xyz as you would expect.
set $newurl $request_uri;
if ($newurl ~ ^/tposreport(.*)$) {
set $newurl $1;
root /var/www/msdsl/tposreport/public;
}
if ($newurl ~ ^/restora(.*)$) {
set $newurl $1;
root /var/www/msdsl/restora/public;
}
# Pass all PHP files to fastcgi php fpm unix socket
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Use php fpm sock which is installed on your machine like php7.2, php5.6
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# Here we are telling php fpm to use updated route that we've created to properly
# response to laravel routes.
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# Deny .ht* access
location ~ /\.ht {
deny all;
}
}`
Just copy and paste the same config into a new file
Name it the same as the subdomain you want it to run on
Give the path to the folder
Add the new subdomain to the hosts file
Restart nginx
Create 2 more servers in nginx. The first for /api (listen en 8080 for example), the other for /web (on 8081). Your main serveur (on 80/443) is then a proxy on the others :
upstream backend_api{
server 127.0.0.1:8080;
}
upstream backend_web{
server 127.0.0.1:8081;
}
server {
listen 80;
server_name www.example.com example.com;
location /api{
include proxy_params;
proxy_pass http://backend_api;
}
location / {
include proxy_params;
proxy_pass http://backend_web;
}
}
server {
listen 8080 default_server;
index index.php index.html index.htm;
root /app/api;
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
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;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M; }
server {
listen 8081 default_server;
index index.php index.html index.htm;
root /app/web;
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
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;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M;
}

Run multiple path under nginx server in local machine, 502 bad gateway when use index.php file as default page

I try to setup a website run with different path into nginx.conf, I have 3 locations testing under the nginx conf, one is a node.js web app, one is a php forum, and the last one is phpmyadmin and it works fine. The problem is on my forum app. the index.php file shows 502 bad gateway when I test it, however it works when I use index.html. Below is configuration file, any idea about this setting?
nginx.conf
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://nodejs;
}
location /forum {
root /parent/path/of/the/folder/;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location /phpmyadmin3 {
root /usr/local/share;
index index.php;
# try_files $uri $uri/ /phpmyadmin/index.php$args$is_args;
location ~ \.php$ {
fastcgi_pass unix:/path/to/.valet/valet.sock;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
UPDATE
nginx.conf
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://nodejs;
}
location /forum {
root /parent/path/of/the/folder/;
index index.php index.html;
}
location /phpmyadmin3 {
root /usr/local/share;
index index.php;
# try_files $uri $uri/ /phpmyadmin/index.php$args$is_args;
}
location ~ \.php$ {
fastcgi_pass unix:/path/to/.valet/valet.sock;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

NGINX FASTCGI/PHP - Primary script unknown while reading response header from upstream, client

I have a development box that each user has a www folder in the home dir. NGINX is hosting those dirs from http://IP ADDRESS/USERNAME. And this works great. I want to get PHP working in the same fashion.
/etc/nginx/sites-available-default:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#root /usr/share/nginx/html;
#index index.html index.htm;
# Make site accessible from http://IP ADDRESS/
server_name IP ADDRESS;
#location ~ ^/(.+?)(/.*)?$ {
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
alias /home/$1/www$2;
index index.html index.htm index.php;
include /etc/nginx/php.fast.conf;
autoindex on;
}
php.fast.conf file:
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /home/$1/www$2$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
As you can see I have tried a few variations but seem to continue to receive the following error in the log: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client.
When attempting to render a simple PHP info page The page displays "file not found"
Other info:
Server = ubuntu 14.x
Latest Nginx
Digital Ocean Droplet
Thanks in advance.
server {
listen 80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~* ^/(.+?)/www(/.*|/|)$ {
root /home;
index index.php index.html;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* ^/(.+?)(/.*|/|)$ {
index index.php index.html;
try_files $uri $uri/ #home;
}
location #home {
rewrite ^/([^/]+?)$ /$1/www/ last;
rewrite ^/(.+?)(/.*|/)$ /$1/www$2 last;
}
}
The cons of this config, you can't use URI like /<anything_you_want>/www.
Maybe it helps. But it's weird, non-optimized and ugly config.

Query strings in NGINX

I have problem with my configuration server on nginx.
My configuration:
server {
listen 80;
server_name shop.md;
index index.php index.html index.htm;
access_log /var/log/nginx/test.dev.access.log;
error_log /var/log/nginx/test.dev.error.log;
location / {
root /home/vagrant/Workspace/shop/web;
try_files $uri $uri/ app_dev.php /app_dev.php$is_args$args;
}
location ~ .php$ {
root /home/vagrant/Workspace/shop/web;
index index.html index.htm index.php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APPLICATION_ENV development;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
}
sendfile off;
}
This configuration allow urls like this :
http://shop.md:8000/1/femei-pantofi
http://shop.md:8000/1/femei-pantofi?min_price=1&max_price=1000
For this URL:
http://shop.md:8000
I get the error 403 Forbidden
I use the #rewriteapp directive of nginx for my Symfony2 projects. The resulting configuration looks somewhat like this:
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|adminer)\.php(/|$) {
fastcgi_pass phpfcgi-siyabonga;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
This works fairly well for me and is taken from the official nginx wiki page about Symfony2 with some additional changes. Also you should check the official Symfony2 docs. They have an example of a correct nginx configuration.

Categories