I want to apply rewrite /author/xxx to /index.php?author_name=xxx
rewrite ^/author/(.*)/$ /index.php?author_name=$1;
When I apply code, browser url looks like that :
http://www.site-name.com/?author_name=xxx
I want url will look like :
http://www.site-name.com/author/xxx but it will rewrite index.php?author_name=xxx
Any idea to fix it?
My Nginx conf file is below :
server {
server_name site-name.com;
rewrite ^(.*) http://www.site-name.com$1 permanent;
listen 8080;
}
server {
listen 8080;
access_log off;
# access_log /home/www.site-name.com/logs/access_log;
error_log on;
# error_log /home/www.site-name.com/logs/error.log;
#add_header X-Frame-Options SAMEORIGIN;
#add_header X-Frame-Options "ALLOW-FROM https://www.site-name.xyz";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
root /home/www.site-name.com/public_html;
include /etc/nginx/conf/ddos2.conf;
# include /etc/nginx/conf/cors.conf;
index index.php index.html index.htm;
server_name www.site-name.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
rewrite ^/author/(.*)/$ /index.php?author_name=$1;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_connect_timeout 250;
fastcgi_send_timeout 250;
fastcgi_read_timeout 250;
fastcgi_buffer_size 256k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME /home/www.site-name.com/public_html$fastcgi_script_name;
}
include /etc/nginx/conf/drop.conf;
}
Your rewrite looks OK: From extern you can use /author/... and internally it will use index.php?....
I suppose you also want to redirect index.php?... to the nice path:
rewrite ^/author/([^/]*)/?$ /index.php?author_name=$1 break;
rewrite ^/index.php$ https://www.example.com/author/$arg_author_name/ redirect;
root /var/www/html/;
fastcgi_index index.php;
fastcgi_pass php;
include fastcgi_params;
Related
I'm trying to get a 404 error page to work if a file isn't found. Currently the server will just display the homepage instead of the 404 page. The rest of the site is behaving normally.
example.com/error-page/404.html // displays 404 page correctly.
I've tried adding the proxy_intercept_errors and fastcgi_intercept_errors directives with no luck.
Thanks for any help!
Config:
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
ssl on;
access_log /var/log/nginx/access.plygrid-LS-com.log;
error_log /var/log/nginx/error.plygrid-LS-com.log;
root /usr/share/nginx/sites/example-com;
server_name example.com;
proxy_intercept_errors on;
index index.html index.htm index.nginx-debian.html index.php;
error_page 404 = /error-page/404.html;
error_page 503 = /error-page/503.html;
location ~ /.well-known {
allow all;
}
location / {
try_files $uri $uri/ =404 #extensionless-php;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
fastcgi_intercept_errors on;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
image/svg+xml;
gzip_vary on;
}
I've managed to fix my issue by:
Adding an index to my main try_files:
location / {
try_files $uri $uri/ =404 #extensionless-php;
index index.html index.htm index.nginx-debian.html index.php;
}
Removing /index.php from my php try_files and adding a =404:
location ~ \.php$ {
try_files $uri $uri/ =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
fastcgi_intercept_errors on;
}
Adding a rewrite to trailing slashes on the php extension remover:
location #extensionless-php {
rewrite ^/(.*)/$ /$1 permanent;
rewrite ^(.*)$ $1.php last;
}
I'm trying to run 2 applications on the server...
first application on the root document (wordpress)
and second application in a folder /app (symfony4)
The problem is that i an't find the way to use the alias on the nested folder...
my config file:
server {
listen 80;
server_name ~^(?<folder>[^.]*).magana.dev.hexis.fr;
charset utf-8;
index index.php index.html index.htm;
root /var/www/projects/dev/magana/$folder/htdocs;
access_log /var/www/projects/dev/magana/$folder/access.log;
error_log /var/www/projects/dev/commons/logs/magana_error.log;
client_max_body_size 200M;
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|js|css)$ {
access_log off;
log_not_found off;
expires 30d;
add_header Pragma "public";
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV magana;
include fastcgi_params;
fastcgi_read_timeout 600;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SERVER_NAME $folder.magana.dev.hexis.fr;
}
location /app {
alias /var/www/projects/dev/magana/$folder/htdocs/app/public;
try_files $uri $uri/ #app;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location #app {
rewrite /app/(.*)$ /app/public/index.php?/$1 last;
}
location ~ /\.ht {
deny all;
}
}
What i'm missing?
Thanks for your help
Whit this code i can access the application but I still don't find the way to declare e=the kernelrootdirectory
server {
listen 80;
server_name ~^(?<folder>[^.]*).magana.dev.example.fr;
charset utf-8;
index index.php index.html index.htm;
set $symfonyRoot /var/www/projects/dev/magana/$folder/htdocs/app/public;
set $symfonyScript index.php;
root /var/www/projects/dev/magana/$folder/htdocs;
access_log /var/www/projects/dev/magana/$folder/access.log;
error_log /var/www/projects/dev/commons/logs/magana_error.log;
client_max_body_size 200M;
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp$
access_log off;
log_not_found off;
expires 30d;
add_header Pragma "public";
}
location /app {
root $symfonyRoot;
rewrite ^/app/(.*)$ /$1 break;
try_files $uri #symfonyFront;
}
location #symfonyFront {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $symfonyRoot/$symfonyScript;
fastcgi_param SCRIPT_NAME /app/$symfonyScript;
fastcgi_param REQUEST_URI /app$uri?$args;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV magana;
include fastcgi_params;
fastcgi_read_timeout 600;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SERVER_NAME $folder.magana.dev.hexis.fr;
}
location ~ /\.ht {
deny all;
}
}
I just made the switch to Nginx after years of using apache. I am in the process of switching everything over but I am having one hell of a time doing so. My current issue is with nagios. I can access nagios but the cgi portion of it does not appear to be working, I just get garbled output. I am also not being prompted for username/password when accessing it which is a bit concerning.
I am also running owncloud on my webserver which seems to be working properly. Here is my configuration. Any help would be greatly appreciated.
upstream php-handler {
server 127.0.0.1:9000;
}
server {
listen 80;
server_name www.<my_server>.com;
return 301 https://$server_name$request_uri;
}
#SSL Configuration
server {
listen 443 ssl;
server_name www.<my_server>.com;
ssl_certificate /etc/letsencrypt/live/www.<my_server>.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.<my_server>.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
include /etc/nginx/default.d/*.conf;
root /mnt/Webserver/html;
client_max_body_size 10G;
fastcgi_buffers 64 4K;
# ownCloud blacklist
location ~ ^/owncloud/(?:\.htaccess|data|config|db_structure\.xml|README) {
deny all;
error_page 403 = /owncloud/core/templates/403.php;
}
location / {
index index.html;
}
location /owncloud/ {
error_page 403 = /owncloud/core/templates/403.php;
error_page 404 = /owncloud/core/templates/404.php;
rewrite ^/owncloud/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/owncloud/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/owncloud/webdav(.*)$ /remote.php/webdav$1 redirect;
rewrite ^(/owncloud/core/doc[^\/]+/)$ $1/index.html;
# The following rules are only needed with webfinger
rewrite ^/owncloud/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/owncloud/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/owncloud/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/owncloud/.well-known/caldav /remote.php/caldav/ redirect;
try_files $uri $uri/ index.php;
}
# Optional: set long EXPIRES header on static assets
location ~* ^/owncloud(/.+\.(jpg|jpeg|gif|bmp|ico|png|css|swf))$ {
expires 30d;
access_log off; # Optional: Don't log access to assets
}
#Nagios
location /nagios {
alias /usr/share/nagios;
auth_basic "Nagios Access";
auth_basic_user_file /etc/nagios/htpasswd.users;
index index.php;
autoindex off;
}
location ~ ^/nagios/(.*\.php)$ {
auth_basic "Nagios Restricted Access (via nginx)";
auth_basic_user_file /etc/nagios/passwd;
root /usr/share/nagios/;
rewrite ^/nagios/(.*) /$1 break;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/share/nagios$fastcgi_script_name;
fastcgi_pass php-handler;
}
location ~ ^/nagios/(.*\.cgi)$ {
auth_basic "Nagios Restricted Access (via nginx)";
auth_basic_user_file /etc/nagios/passwd;
root /usr/lib64/nagios/cgi;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
include /etc/nginx/fastcgi_params;
fastcgi_param AUTH_USER $remote_user;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param SCRIPT_FILENAME /usr/lib64/nagios/cgi$fastcgi_script_name;
fastcgi_pass php-handler;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS on;
fastcgi_pass php-handler;
}
}
Any help would be greatly appreciated.
I figured it out. Here is my new configuration. Thanks.
upstream php-handler {
server 127.0.0.1:9000;
}
server {
listen 80;
server_name www.<my_server>.com;
return 301 https://$server_name$request_uri;
}
#SSL Configuration
server {
listen 443 ssl;
server_name www.<my_server>.com;
ssl_certificate /etc/letsencrypt/live/www.<my_server>.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.<my_server>.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
include /etc/nginx/default.d/*.conf;
root /mnt/Webserver/html;
client_max_body_size 10G;
fastcgi_buffers 64 4K;
# ownCloud blacklist
location ~ ^/owncloud/(?:\.htaccess|data|config|db_structure\.xml|README) {
deny all;
error_page 403 = /owncloud/core/templates/403.php;
}
location / {
index index.html;
}
location /owncloud/ {
error_page 403 = /owncloud/core/templates/403.php;
error_page 404 = /owncloud/core/templates/404.php;
rewrite ^/owncloud/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/owncloud/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/owncloud/webdav(.*)$ /remote.php/webdav$1 redirect;
rewrite ^(/owncloud/core/doc[^\/]+/)$ $1/index.html;
# The following rules are only needed with webfinger
rewrite ^/owncloud/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/owncloud/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/owncloud/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/owncloud/.well-known/caldav /remote.php/caldav/ redirect;
try_files $uri $uri/ index.php;
}
# Optional: set long EXPIRES header on static assets
location ~* ^/owncloud(/.+\.(jpg|jpeg|gif|bmp|ico|png|css|swf))$ {
expires 30d;
}
#Nagios
location /nagios {
alias /usr/share/nagios;
auth_basic "Nagios Restricted Access (via nginx)";
auth_basic_user_file /etc/nginx/.htpasswd;
index index.php;
autoindex off;
}
location ~ ^/nagios/(.*\.php)$ {
root /usr/share/nagios/;
rewrite ^/nagios/(.*) /$1 break;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/share/nagios$fastcgi_script_name;
fastcgi_pass php-handler;
}
location ~ ^/nagios/(.*\.cgi)$ {
root /usr/lib64/nagios/cgi;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
include /etc/nginx/fastcgi_params;
fastcgi_param AUTH_USER $remote_user;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param SCRIPT_FILENAME /usr/lib64/nagios/cgi$fastcgi_script_name;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS on;
fastcgi_pass php-handler;
}
}
I am using Red Hat Enterprise server for hosting my phalcon based application. But after deployment the application is not working and showing "Please enable rewrite module on your web server to continue". I am using the below configuration in my default.conf file.
If any body has any idea plz help me to resolve the issue.
server {
listen 80;
server_name example.com www.example.com;
access_log /srv/www/example.com/log/access.log;
error_log /srv/www/example.com/log/error.log;
root /srv/www/example.com/public/;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ #php_mvc;
}
location #php_mvc {
rewrite ^(.+)$ /index.php$1 last;
}
location ~ ^(.+\.php)(/.*)?$ {
fastcgi_split_path_info ^(.+\.php)(/.*)?$;
set $script_filename $document_root$fastcgi_script_name;
if (!-e $script_filename) {
return 404;
}
fastcgi_pass fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param APPLICATION_ENV development;
fastcgi_param SCRIPT_FILENAME $script_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Can you please try to add following code in your nginx configuration and check it again.
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=$1;
}
This will work for you. (timeouts are high, you should change it for your app specs)
server {
listen 80 default_server;
server_name _;
client_max_body_size 128M;
location / {
root /var/www/public;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
client_max_body_size 128M;
proxy_read_timeout 3000;
}
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=$uri&$args;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /var/www/public;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
location ~ "\.(js|ico|gif|jpg|png|jpeg|xls|csv)$" {
root /var/www/public;
}
location ~* \.(jpg|jpeg|png|gif|ico)$ {
expires 365d;
log_not_found off;
access_log off;
}
}
I've been struggling with this for some time now. I want to setup a Wordpress blog to run from a "/blogname" path on a server instead of the root. I also want the path to have a different name then the directory where the Wordpress scripts are since the server itself will run django.
I have Nginx as a reverse proxy and I set up php-fpm to run the wordpress. Here's my Nginx configuration file:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#tcp_nopush on;
#gzip on;
server {
root /Users/username/Dev/Wordpress/;
index index.php index.html index.htm;
listen 8080;
server_name localhost;
# Do not serve hidden files
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
# Static files
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
# This is the problem
location /blogname {
try_files $uri $uri/ /index.php;
rewrite /blogname(.*) /blog$1 last;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Users/username/Dev/Wordpress/blog$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
}
Right now when I visit localhost:8080/blogname I just download the index.php script instead of executing it.
Other tips are also welcome.
Replace this
location /blogname {
try_files $uri $uri/ /index.php;
rewrite /blogname(.*) /blog$1 last;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Users/username/Dev/Wordpress/blog$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
with this
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
}
I hope this should work for you because this is what I use for my local server.