Nginx allow user agent specific directory - .htaccess

I migrate apache to nginx.
In apache I put .htaccess inside folder, but nginx hasn't this.
I have folder /admin and it can only by accessed by User Agent: Administrator
I do:
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
And it works, but .php files isn't interpreted.
How can I do this?
Edit:
server {
listen 80;
server_name myserver.com;
root /usr/share/nginx/html;
location / {
}
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
include /etc/nginx/default.d/*.conf;
}

There is problem with the specification about agent callback instead of "Administrator" it should be (Administrator) rest of the code seems to be ok. If you have added try_files $uri =404; additionally then remove it as it don't required. return 404 already defined.
Try this one
server {
listen 80;
server_name myserver.com;
root /usr/share/nginx/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
}
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
include /etc/nginx/default.d/*.conf;
}
restart nginx service before test!

Related

ngnix issue with chrome redirection non-www to www

I have done basic configuration for my site to run using nginx,
server {
listen 80;
root /home/htdocs/;
index index.html index.htm index.php;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ /index.php?$args /;
}
# added error page
error_page 404 = #notfound;
location #notfound {
return 301 /;
}
location ~ \.php$ {
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 443 ssl http2;
#listen [::]:443 ssl;
# SSL
ssl_certificate /etc/letsencrypt/live/example.com-0002/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com-0002/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com-0002/chain.pem;
ssl_protocols TLSv1.2 TLSv1.3;
root /home/htdocs/;
index index.html index.htm index.php;
server_name www.example.com;
#return 301 https://$server_name$request_uri;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ /index.php?$args /;
}
# added error page
error_page 404 = #notfound;
location #notfound {
return 301 /;
}
location ~ \.php$ {
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Whats wrong with above config even I tried in incognito mode of google-chrome but still same.
Hope to hear from you with solution if I'm doing any thing wrong.
Regards
I have tried different solutions and parameters noting work out for me.

Nginx Rewrite Not working

I have move my site to nginx but can get .htaccess roules right
below ius my code
server {
listen 80;
server_name mysite.com;
root /usr/share/nginx/mysite;
index index.php index.html index.htm;
location / {
rewrite ^/(.*)$ /members.php?id=$1 last;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
i want to display user url to be
mysite.com/jonny
instead of
mysite.com/memner.php?id=jonny
when i use
rewrite ^/(.*)$ /members.php?id=$1 last;
site don't load please help.
got fix for this
if (!-e $request_filename) {
rewrite /(.*) /members.php?id=$1 last ;
}

nginx rewrite & auth_basic (modx cms)

I'd like to protect the root with passwd and make the urlfriendly rewrite for modx cms.
only the rewriting works but the password won't be requested.
my ispconfig nginx directives looks like this
location / {
index index.html index.php
auth_basic "Protected Area";
auth_basic_user_file /var/www/clients/client21/web22/web/htpasswd;
client_max_body_size 0;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 600;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
client_max_body_size 0;
}
seems like the auth_basic won't be executed or overwriten by the rewrite rule. anyone got a idea?
after alot of ppl viewing this question but i didn't received a answer here i post the solution to the problem. The problem was that the basic auth needs to be done in the php directive.
location / {
index index.html index.php
client_max_body_size 0;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
auth_basic "Protected Area";
auth_basic_user_file /var/www/clients/client21/web22/web/htpasswd;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 600;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
client_max_body_size 0;
}
Try this set of rules. No PHP directives and leverages FURLS.
location / {
auth_basic "Restricted";
# Add path to .htpasswd file
auth_basic_user_file /var/www/clients/client21/web22/web/.htpasswd;
# if the protected location is modx, then ...
try_files $uri $uri/ #modx-rewrite;
# otherwise, if static files, you’d use
# try_files $uri $uri/ =404;
# This line should always be placed at the end
try_files $uri $uri/ #modx-rewrite;
}

nginx GET .php variables

I have a bunch of PHP files that take .php?id=123 and I need to get them all. How do I do them all in my config file?
I can't seem to figure out how to make use of
get1.php?id=stuff
get2.php?id=stuff
get3.php?id=stuff
and so on...
The problem is how do I do that when they are all under the same root directory?
With the following I get 500 ERROR on the p.php?id=945 but PHP works fine but I CANT login or get POST data to work
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
location / {
index index.php index.html index.htm;
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 1d;
try_files $uri?$args #backend;
}
error_page 405 = #backend;
add_header X-Cache "HIT from Backend";
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #backend {
internal;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(php|jsp|cgi|pl|py)?$ {
try_files $uri?$args /index.php;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
THIS: just 500s
rewrite or internal redirection cycle while internally redirecting to "/index.php"
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
#try and serve static files directly
location ~* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
try_files $uri #inPlaceDynamicFile;
expires 24h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
#allow us to have dynamic css/js files
location #inPlaceDynamicFile {
# examples /css/cssIncludes.css => /css/cssIncludes.css.php
try_files $uri.php =404;
fastcgi_pass 127.0.0.1:9001;
include fastcgi_params.conf;
}
location / {
try_files $uri?$args /index.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
}
"Is this correct?"
Nope.
1) Root should never be in a location block, it should only be in a server block.
2) Apache rewrite for testing if something is an existing file is done with try_files not with Nginx rewrite. See also
3) Your proxy_pass is going to be passing through to the same server which has got to be a circular redirect.
4) You've also got a location block inside a location block, which seems quite odd. Although that may be needed for some advanced config, you don't need it for what you're doing.
I think you probably want you nginx conf to be like this:
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
#try and serve static files directly
location ~* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
try_files $uri #inPlaceDynamicFile;
expires 24h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
#allow us to have dynamic css/js files
location #inPlaceDynamicFile {
# examples /css/cssIncludes.css => /css/cssIncludes.css.php
try_files $uri.php =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params.conf;
}
location / {
try_files $uri /p.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
}
If you do want the only requests that contain .php to go to your p.php file then you should replace the last location block with:
location ~ /(.*)\.php {
try_files $uri /p.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
location / {
try_files /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
Edit
Do I need to create specific rewrites for EVERY php file that has a
$_GET[''];?
No - you should be able to pass them in to any file with the try_files like:
location / {
try_files $uri?$args /index.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
That would match any .php request to a php file if it exists with the query string appended , and then fall back to index.php if the .php file doesn't exist with both the query string and path passed in.

FastCGI - Rewrite - Location with Nginx

I just switched to nginx but I'm having trouble with my URL-Rewriting
I used
location /id/ {
rewrite ^/id/(.*) /index.php?id=$1 break;
}
But the php code is not interpreted, worst it's downloaded raw.
Yet .php files are configured as follow:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/my_app$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
What is wrong with my vhost ?
EDIT: Here is the entire vhost
server {
listen 80; ## listen for ipv4
server_name viditx.com www.viditx.com; ## change this to your own domain name
# I find it really useful for each domain & subdomain to have
# its own error and access log
error_log /var/log/nginx/viditx.com.error.log;
access_log /var/log/nginx/viditx.com.access.log;
root /var/www/viditx;
location / {
# Change this to the folder where you want to store your website
index index.html index.htm index.php;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
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 /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# again, change the directory here to your website's root directory
# make sure to leave $fastcgi_script_name; on the end!
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Did you read the documentation?
break -> last:
location /id/ {
rewrite ^/id/(.*)$ /index.php?id=$1 last;
}
You should try :
location /id/ {
rewrite '^/id/(.*)$' /index.php?id=$1 break;
}
and :
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
Have fun !

Resources