mardi 4 juillet 2017

serve multi projects using nginx in single domain

I have website and i want to add my webservice for applications. my webserver is nginx and both of my server codes are laravel(one for website and other for applications).

the problem is that i cant serve my requests through my new folder that contains my api application code.

my website is example.com and it serves from correct root in nginx, but i want my applications to use this url : example.com/v1/...

so basically i need all requests to exmaple.com/anything except v1 to use /var/www/example and all requests to http://ift.tt/2sle1vm to use /var/www/api-exmaple .

BTW they both needs to serve laravel requests so it needs to forward any urls to index.php (some kind of htaccess).

below is my nginx config that is correct for my exmaple.com ,and i need to modify this to serve example.com/v1 too.

they are not subfolders and both of them are next to eachother.

as u can see i have tried something in location ^~ /v1 {} part but didnt work for me. Thanks

server {

listen 185.88.154.21:443;
    pagespeed MapOriginDomain "http://localhost" "https://www.example.com";
pagespeed LoadFromFile "https://www.example.com" "/home2/example/";

ssl on;
    ssl_certificate /etc/nginx/ssl/certs/example.crt;
ssl_certificate_key /etc/nginx/ssl/keys/example.key;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
server_name   www.example.com;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;

error_log   /var/log/nginx/example.error.log debug;
    root   /var/www/example/public_html;
    index  index.php index.php5 index.html index.htm;

location / {
     try_files $uri $uri/ /index.php$is_args$args;
}

location ^~ /v1 {
root   /var/www/example/public;
 try_files $uri $uri/ @api-example;
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    #try_files $uri =404;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

     fastcgi_index index.php;

    include fastcgi_params;
    add_header Access-Control-Allow-Origin *;
      }
       }


   location @api-example {
rewrite /v1/(.*)$ /api-example/index.php?/$1 last;
     }

location ~ \.php$ {
    root  /var/www/example/public_html;
     fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #try_files $uri =404;
     fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

    fastcgi_index index.php;

 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    add_header Access-Control-Allow-Origin *;
   }

error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}




location ~ \.(css)$ {
expires     30d;
gzip on;
}

location ~ \.(gif|ico|ttf|woff|eot|woff2)$ {
expires     60d;
gzip on;
}

#location ~ \.(png|jpg|js)$ {
#expires        7d;
#gzip on;
#}

 }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire