I am using basic template of yii2.0.3 on a nginx server and have a demo theme under web/theme/demo.
I have config the web.php as below for the theme.
'components' => [
'view' => [
'theme' => [
'pathMap' => ['#app/views' => 'theme/demo'],
'baseUrl' => 'theme/demo'
]
],
Everything is working fine.I want to remove the web/index.php from the url of the home page as well as from other pages too.
As nginx does not support .htaccess i have put simple the below rules on the web.php file
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [],
],
I have not put anything inside 'rules'..so clean url may not working properly.
Please help me to remove /web/ from each page of the template.
Check the server block in Nginx configuration as following:
server{
listen 8082;
server_name yii2.dev;
access_log logs/yii2.access.log;
error_log logs/yii2.error.log error;
root /home/admin/web/nginx/html/basic/web/;
location / {
index index.html index.php;
if (!-e $request_filename){
rewrite ^/(.*) /index.php?r=$1 last;
}
}
}
Here's the official config from the nginx website, dedicated for Yii, so you know it's the right way (It seems to be for Yii1, but we can adjust some elements for Yii2:
server {
server_name domain.tld;
root /var/www/html;
index index.html index.php;
# BEGIN Yii Specific location configurations
# SEF URLs for sampleapp.
location /{
rewrite ^/(.*)$ /index.php?r=$1;
}
location ~ \.(js|css|png|jpg|jpeg?|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
# END Yii Specific specific location configurations.
# IMPORTANT: THIS SECTION MIGHT BE DIFFERENT DEPENDING ON IF
# YOU USE PHP-FPM, PHP-FASTCGI, ETC
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
}
Related
After installing Owncloud on my server, on a subdomain (let's say) cloud.motherboard.fr, I have the issue that all my https requests (like https://hey.motherboard.fr) redirect to the following Owncloud page :
while going to https://hey.motherboard.fr
So I guess that my Nginx configuration redirect all https connections to Owncloud. Here is my configuration file :
upstream php-handler {
server 127.0.0.1:9000;
# server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name cloud.motherboard.fr; #YourIP or domain
return 301 https://$server_name$request_uri; # redirect all to use ssl
}
server {
listen 443 ssl;
server_name cloud.motherboard.fr; #YourIP or domain
#SSL Certificate you created
ssl_certificate /etc/nginx/cert/owncloud.crt;
ssl_certificate_key /etc/nginx/cert/owncloud.key;
# owncloud path
root /var/www/cloud/owncloud/;
client_max_body_size 10G; # set max upload size
fastcgi_buffers 64 4K;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
location / {
# The following 2 rules are only needed with webfinger
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ index.php;
}
location ~ ^(.+?\.php)(/.*)?$ {
try_files $1 = 404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$1;
fastcgi_param PATH_INFO $2;
fastcgi_param HTTPS on;
fastcgi_pass php-handler;
}
# Optional: set long EXPIRES header on static assets
location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
# Optional: Don't log access to assets
access_log off;
}
}
It looks like it comes from the two first server block, but I didn't managed to change it. Can it have a link with php-pfm ?
My hey.motherboard.fr configuration is pretty simple :
server {
server_name hey.motherboard.fr;
location / {
root /var/www/hey;
index index.html index.htm;
}
}
I would like to password protect one of the URLs I have and I am trying to do it with:
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/my.passwd;
}
The problem is that I am asked for the username and paasword. AS soon as I put the right username and password, I am getting a 404 Error with this log:
*55268 open() "/var/www/mysite.com/deployment/web/about/payment" failed (2: No such file or directory), client: 172.16.0.53, server: ~^(?<branch>\w+)\.mysite\.dev$, request: "GET /about/payment HTTP/1.1", host: "deployment.mysite.dev"
EDIT:
The entire nginx conf file is here
server {
listen 80;
access_log ...;
error_log ...;
server_name ~^(?<branch>\w+)\.mysite\.dev$ ~^(?<branch>\w+)\.mysite\.com$;
root /var/www/git/branches/mysite.com/$branch/web;
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/mysite.passwd;
}
# strip app_eudev.php/ prefix if it is present
rewrite ^/app_eudev\.php/?(.*)$ /$1 permanent;
# remove trailing slash
rewrite ^/(.*)/$ /$1 permanent;
# sitemap rewrite
rewrite ^/sitemap_(.*)$ /sitemap/$1 last;
location / {
try_files $uri #symfonyapp;
}
location #symfonyapp {
rewrite ^(.*)$ /app_eudev.php/$1 last;
}
location /var/www/dms/ {
internal;
alias /var/www/dms/;
}
location #htmlimages {
root /var/www/dms/;
}
location ~ /html/.*\.(png|gif|jpg|pdf)$ {
root ...;
try_files $uri #htmlimages;
}
location /files {
root ...;
}
location /assets {
root ...;
}
location /img {
root ...
}
location ~ \.php(/|$) {
fastcgi_pass 127.0.0.1:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
When nginx find matching location that will process request it ignores any other location that could possibly match request.
In your case before you add auth, request to /about/payment was proceeded by location / which finally passed request to PHP. But as soon as you add location /about/payment request to that URL will be processed by this location which has no special directives so nginx will try to serve static files.
You should add directives that pass request to PHP, in this case it's really simple:
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/my.passwd;
root ...;
try_files $uri #symfonyapp;
}
E.g. for Wordpress site, I wanna lock the URL
location ~* /block-url/ {
auth_basic "Internal Staging Site";
auth_basic_user_file /etc/nginx/.htpasswd;
try_files $uri $uri/ /index.php?$args; # Most important!
}
I need to make url rewrite rules (server blocks) in nginx server same like in my previous apache server.
This is code from .htaccess what I need to implement (convert) into my existing one:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ admin/index.php?hotelname=$1&do=$2 [QSA]
RewriteRule ^(([A-Za-z0-9-/]+)+)$ admin/index.php?hotelname=$1 [L]
This code is in my website because I need to hide in adress bar folder(/admin/) where is located files after login. And when somebody is already login, adress bar is like www.domain.com/username and when you click to menu adress is like www.domain.com/username/page1, www.domain.com/username/page2, www.domain.com/username/page3.
This is what I need to achive in nginx. Because now is complete backend without function. When I login to backend, I'm redirected to www.domain.com/username but on screen I can see only File not found. In backend working only when I manualy add www.domain.com/admin/index.php.
This is my actual config for nginx :
server_names_hash_bucket_size 64;
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
root /usr/share/nginx/www;
index index.php;
server_name www.example.com;
error_page 404 http://www.example.com/404.php;
autoindex off;
location / {
rewrite ^([^\.]*)$ /$1.php;
}
location = / {
rewrite ^ /index.php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
When I try to change my block to:
location / {
rewrite ^([^\.]*)$ /$1.php;
rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ /admin/index.php?hotelname=$1&do=$2;
rewrite ^/(([A-Za-z0-9-/]+)+)$ /admin/index.php?hotelname=$1 break;
}
Each my css file have error 500...
I will be very grateful with any help!
Thanks a lot.
You are putting this in your / location, that means that all your requests not matched before you drop here. You have to create a location role specific for this entry before the location /
location ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ {
rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ /admin/index.php?hotelname=$1&do=$2;
}
I have problem with implement my rewrite code from htaccess file into nginx config.
I've already tried generator : http://winginx.com/htaccess for generate my rewrite
code.
My nginx config code:
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
root /usr/share/nginx/www;
index index.php;
server_name www.example.com;
error_page 404 http://www.example.com/404.php;
autoindex off;
error_log /usr/share/nginx/www/nginx_error.log warn;
location / {
rewrite ^([^\.]*)$ /$1.php;
}
location = / {
rewrite ^ /index.php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
I wanna implement this from my .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ admin/index.php?hotelname=$1&do=$2 [QSA]
RewriteRule ^(([A-Za-z0-9-/]+)+)$ admin/index.php?hotelname=$1 [L]
Generated code from tool:
location / {
rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ /admin/index.php?hotelname=$1&do=$2;
rewrite ^/(([A-Za-z0-9-/]+)+)$ /admin/index.php?hotelname=$1 break;
}
I have alredy tried implement this last lines of code to my location blocks but not working at all..
I will be very greateful for every opinion!
Regards
Makromat
The blind kind of conversion would be
rewrite ^([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ admin/index.php?hotelname=$1&do=$2&$query_string last;
rewrite ^(([A-Za-z0-9-/]+)+)$ admin/index.php?hotelname=$1 last;
But I would prefer if I understand the question more to produce a more optimum rewrite.
When do I know if the URL should be passed to /admin or not, give me an actual URI for backend and for frontend.
Usually rewrites are better managed in nginx using nginx way of thinking. And this new way of thinking is more based on try_file.
So you may try something like that (untested):
location ^~ "/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)" {
try_files $uri admin/index.php?hotelname=$1&do=$2&$args;
}
location ^~ "(([A-Za-z0-9-/]+)+)" {
try_files $uri /admin/index.php?hotelname=$1;
}
location = / {
rewrite ^ /index.php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
If direct access on given $urishould never happen, then remove that part from the try_files. Now I'm also unsure of your second regex (([A-Za-z0-9-/]+)+), why not using:
location ^~ "/([A-Za-z0-9-/])+"
Or
location ^~ "/([A-Za-z0-9-])+/"
So there's maybe something I do not see, even in your apache rewrites.
I have a nginx server running with ssl enabled.
Currently I have https enabled for all directories.
How enable ssl only for www.example.com/shop/* directory and disable for other??
here is my conf file :
# Redirect everything to the main site.
server {
server_name *.example.com;
listen 80;
ssl on;
ssl_certificate /opt/nginx/conf/server.crt;
ssl_certificate_key /opt/nginx/conf/server.key;
keepalive_timeout 70;
access_log /home/example/nginx_logs/access.log ;
error_log /home/example/nginx_logs/error.log ;
root /home/example/public_html/example.com;
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args;
root /home/example/public_html/example.com/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /opt/nginx/conf/fastcgi_params;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /home/example/public_html/example.com$fastcgi_script_name;
index index.php index.html index.htm;
}
if ($http_host != "example.com") {
rewrite ^ http://example.com$request_uri permanent;
}
include global/restrictions.conf;
# Additional rules go here.
#Only include one of the files below.
include global/wordpress.conf;
# include global/wordpress-ms-subdir.conf;
# include global/wordpress-ms-subdomain.conf;
}
tnanks,
D
It is easy to achieve in Nginx. There are two steps involved in it.
Port 443 will be used only when yourdomain.com/shop is accessed. All other requests would be redirected to port 80 (HTTP)
Port 80 will check for yourdomain.com/shop. If found, it'd be redirected to port 443 (HTTPS).
Here is a quick overview of how it could be done...
server {
listen 443;
server_name yourdomain.com;
# directives for SSL certificates
# root, index, error_log, access_log directives
location /shop {
# directives to handle what's inside /shop, for example
# try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
# directives to handle PHP files
}
# leave everything else to port 80
location / {
rewrite ^ http://$host$request_uri permanent;
}
}
server {
listen 80;
server_name yourdomain.com;
# root, index, error_log, access_log directives
# redirect yourdomain.com/shop to port 443
# Please put this before location / block as
# nginx stops after seeing the first match
location /shop {
rewrite ^ https://$host$request_uri permanent;
}
location / {
# directives to handle what's inside /, for example
# try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
# directives to handle PHP files
}
}