I still struggling with making mystaticusername variable user name. I mean when I have particular username /statisusername/ backend working properly on nginx. But how can I make my configuration for more users? Means use instead mystaticusername some redirection where each username will know login to backend.
How can I change my nginx configuration?
server {
server_name example.com www.example.com;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/www;
charset utf-8;
index index.html index.php /index.php;
location = / {
rewrite ^ /index.php;
}
location / {
rewrite ^([^\.]*)$ /$1.php;
rewrite ^/([A-Za-z0-9_]+)$ /admin/index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location /mystaticusername {
try_files $uri/ /admin/index.php?q=$uri&$args;
}
location = /mystaticusername/options {
try_files $uri $uri/ /admin/index.php?hotelname=$1&do=options;
}
}
Not much context given, so I'm just guessin... but looks like you could condense your location directives to 3 statements.
This will default to index.php, but in the case of example.com/mystaticusername if would try that url and since it does not exist (guessing) it would serve up /admin/index.php?q=$uri&$args. This would also allow urls like exmaple.com/about to resolve. Just make certain to not allow users to create a username of 'about'
location / {
try_files $uri $uri/ /admin/index.php?q=$uri&$args;
}
Not certain what your goal is, but this would get you example.com/mystaticusername/options to try serve up /admin/index.php?hotelname=$1&do=options
location ~* ^/[a-zA-Z0-9]*/options$ {
try_files $uri /admin/index.php?hotelname=$1&do=options;
}
Leave php as-is
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Related
I recently moved a website which used Apache to Nginx.
How can I get an url which looks like this:
http://example.com/login
to point to http://example.com/login.php while keeping the .php out of the url?
My current configuration:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html/;
index index.php index.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
I usually use something like this and it's SEO proof. Tested by a lot of my client sites, works like a charm.
In your /etc/nginx/conf.d/domain.tld.conf file include this :
server {
index index.html index.php;
location / {
if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
try_files $uri $uri/ $uri.html $uri.php?$args;
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 302 /$1?$args; }
try_files $uri =404;
# add fastcgi_pass line here, depending if you use socket or port
}
}
Source i used a while a go
I'm using something like the following as a "default" vhost on Nginx. I want every sub domain to have an own directory basically.
Can anyone help with a fallback (I'm new to this). If a directory/sub domain doesn't exists I want some kind of custom 404.html page.
Thanks!
server {
server_name ~^(.+).mysite.com$;
set $root_path $1;
root /var/www/$root_path/public;
index index.html index.php;
location / {
try_files $uri /$uri /index.php?$args;
}
location ~ \.php$ {
#Include Nginx’s fastcgi configuration
include /etc/nginx/fastcgi.conf;
#Look for the FastCGI Process Manager at this location
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
client_max_body_size 100m;
}
}
Try this:
server {
...
error_page 404 /your_custom_404.html;
location = /your_custom_404.html {
root /path/to/your_custom_404.html;
internal;
}
location / {
try_files $uri $uri/ /index.php?$args =404;
}
...
}
I've read a moderate amount of the documentation for location blocks, but I dont have much experience with RegEx so I am a bit lost on how to pull off what I am trying to do. The following nginx config will probably explain what I want to do better than I can word it:
server {
server_name example.com www.example.com;
root /var/www/;
index index.php;
location /blog/ {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location / {
try_files $uri #uwsgi;
}
location #uwsgi {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
example.com/ is being served by a bottle app through uwsgi, and so all things under this location should be routed to the bottle app and handled there. This is working fine as expected, however I am lost on how to add an 'exception' to the location rule so that example.com/blog, and everything under it ../sub1/sub2 etc. are not directed to the bottle app, but infact handled by wordpress and its PHP magic.
This seems like it should be very simple to set up, but it's proving very difficult to google simple solutions to these sort of problems, as everyone seems to bloat thier 'tutorial' configurations with tons of non-essentials that confuse a beginner.
This may need some tweaks, but you should probably use a nested location block:
server {
server_name example.com www.example.com;
root /var/www;
index index.php;
location /blog/ {
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
location / {
try_files $uri #uwsgi;
}
location #uwsgi {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
Notice that the default URI is changed to /blog/index.php which is hopefully where all of your WordPress files are located.
How can I solve this problem: I want to set up nginx conf file to meet below criteria:
http://www.example.com/site1/article/index.php?q=hello-world -> http://www.example.com/site1/article/hello-world
httb://www.example.com/site2/article/index.php?q=goodbye-world -> httb://www.example.com/site2/article/goodbye-world
httb://www.example.com/site3/article/index.php?q=open-new-world -> httb://www.example.com/site3/article/open-new-world
There are multiple sites after example.com, I want to make the url look clean by using nginx configuration.
But my below configuration doesn't work. Someone help me?
server {
listen 80;
listen [::]:80;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
server_name www.example.com;
location ~ /article/ {
try_files $uri /site1/article/index.php?q=$1;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
You would like the client to present a URL like /xxx/article/yyy which is then internally rewritten to /xxx/article/index.php?q=yyy.
You need to capture the components of the source URI in order to use them later. You have a $1 in your question, but you are missing the expression to actually give it a value. With the minimum number of changes, this works:
location ~ ^(.*/article/)(.*)$ {
try_files $uri $1index.php?q=$2;
location ~ \.php$ { ... }
}
However, you do not need to use a nested location for PHP, as long as the PHP regex location appears above the other regex location, it will process all php files. For example:
location ~ \.php$ { ... }
location ~ ^(.*/article/)(.*)$ {
try_files $uri $1index.php?q=$2;
}
Both landing.php and all the other pages of my site are in the same directory. I want to be able to access both, however if I have a try_files statement as well as an index statement, I get a 404 error when accessing the index page.
Is there a way to achieve this without conflicting either?
Here's my configuration file:
server {
listen 80;
access_log /var/www/mysite.com/logs/access.log;
error_log /var/www/mysite.com/logs/error.log;
root /var/www/mysite.com/public_html/mysite/public/_main;
server_name www.mysite.com mysite.com;
location / {
index landing.php;
try_files $uri $uri.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index landing.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I finally worked out how to do this. What you need is two location blocks, like this:
location = / {
index landing.php;
}
location / {
try_files $uri $uri.php?$query_string;
}
What this does is use try_files for any other page such as www.site.com/somepage; however if the URL is www.site.com, it will fetch the index page.
Try the following config it worked for me:
server {
root /path/to/root;
index index.html index.htm;
server_name subdomain.domain.com;
location / {
try_files $uri $uri/ /index.html;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
}
}