Nginx alias configuration for API with Silex - api

I've a problem with my NGinx configuration, I know there is a lot of topics already, but I didn't manage to my working properly...
So I've mydomain.com and mydomain.com/api/, my folders are like:
/var/www/mydomain.com/website/dist and /var/www/mydomain.com/api/public
And at the moment, I've something almost working. But the index.php still get the /api part in my routes.
server {
listen 80;
server_name mydomain.com www.mydomain.com;
index index.php index.html;
location / {
root /var/www/mydomain.com/website/dist;
}
location ~ ^/api(/|$) {
root /var/www/mydomain.com/api/public;
try_files $uri /api/index.php;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV production;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Content-Type,Authorization';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
}
access_log /var/logs/nginx/logs/mydomain.com-access.log;
error_log /var/logs/nginx/logs/mydomain.com-error.log;
}
I guess I'm not understanding something with the location or with php-fpm. What did I miss ? What should I do to get this working properly ?
I read too that a try_file is enought for that, than a rewrite rule... I don't know :/
Thank you for your help !

Related

Nginx Redirect https://non-www to http://www not working

I've spend a bit of time searching for an answer to this but can't seem to lock it down and any help would be appreciated.
I've setup SSL on an Nginx server but can't seem to get the non-www https domain to redirect to the www https domain.
I am trying to fix this:
https://example.com -> https://www.example.com
Below is my current config:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
access_log /var/log/nginx/site.access.log;
error_log /var/log/nginx/site.error.log;
server_name example.com www.example.com;
root /var/www/site;
index index.php index.html index.htm;
client_max_body_size 10m;
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
# catch all
error_page 404 /index.php;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/site$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
location = /xmlrpc.php {
deny all;
}
location ~* \.html$ {
expires -1;
}
location ~ \.(htc|less|js|js2|js3|js4)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
try_files $uri $uri/ $uri.html /index.php?$args;
}
location ~ \.(css|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
expires 3600s;
add_header Pragma "public";
add_header Cache-Control "max-age=3600, public";
try_files $uri $uri/ $uri.html /index.php?$args;
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|_ttf|wav|wma|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|zip)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
try_files $uri $uri/ $uri.html /index.php?$args;
}
location ~* \.(eot|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}
listen 443 ssl; # managed by Certbot
listen [::]:443 ssl;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
None of the below answers have resolved my issue's. I am currently hosting the site on AWS could it be the way I have setup my bucket to redirect to the domain?

nginx dont pass SSL parameter for default page

I am facing this weired issue.
After a long trial and error I concluded that. My nginx server is not passing the SSL parameter to PHP if the user visits the Home page first time.
it works well if the user clicks some other links.
it works well when user first time visit other links.
it works well even for Home page second time.
To check please visit my site test.jawabee.in, here you can search for [SSL_PROTOCOL] => TLSv1.2 which is absent if you visited home page first time.
To test again, kill your browser forcefully (from task manager)
Here is my nginx.comf.
server {
listen 443 ssl ;
server_name test.jawabee.in;
root /usr/share/nginx/jawabee;
ssl_certificate /etc/letsencrypt/live/test.jawabee.in/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/test.jawabee.in/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/test.jawabee.in/chain.pem;
ssl_session_cache off;
include /etc/nginx/conf.d/testjawabee.template;
}
here is my testjawabee.template
index index.php index.html index.htm;
location ~ (^\.|/\.) {
return 403;
}
location / {
try_files $uri $uri/ #elgg;
}
location ~ \.php$ {
try_files $uri #elgg;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location #elgg {
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param SSL_PROTOCOL $ssl_protocol;
fastcgi_param QUERY_STRING __elgg_uri=$uri&$args;
}

Phalcon route not working, all urls access index

I use phalcon-dev-tools-v2.0.13 create a project named test,I accessed root url "/",it worked!then,I change some code like this:
it worked!
but:
i add another url ,it didn't work!
what's the matter? I'm a Phalcon fresher,i followed the Phalcon doc.
My system is php5.6.30 Phalcon2.0.13, nginx conf file is:
server {
listen 80;
server_name test;
root /Users/ryugou/test/public;
index index.php index.html index.htm;
charset utf-8;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
your problem is on nginx configuration file. You are not passing _url on location block-
modify location block as (careful last part)-
location / {
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
check Phalcon Nginx Config

Nginx Configuration/Rewrite

I want to configure nginx to behave in this way :
[OK] If i browse to domain.com/, the /var/www/index.php file is called
[HOW ?] If i browse to domain.com/blah, /var/www/controller.php is called
On apache, is done by a rewrite condition :
RewriteRule (.*) controller.php [L,QSA]
[HOW ?] If i browse to domain.com/api/someMethod, /var/www/api/controller.php is called
On apache, is done by a rewrite condition :
RewriteRule ^api api/controller.php [L,NC]
[HOW ?] If i browse to domain.com/image.png, nginx display the image /var/www/image.png
On apache, is done by a rewrite condition :
RewriteRule \.(js|css|gif|png|jpg|ico|txt|woff|woff2)$ - [L,NC]
And my nginx config :
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;
listen [::]:443 ssl;
server_name _;
root /var/www;
index index.php;
ssl_certificate /etc/ssl/certs/server.crt;
ssl_certificate_key /etc/ssl/private/server.key;
location ~ {
try_files $uri $uri/ /index.php;
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;
}
}
The significant parts of the configuration would be:
root /var/www;
index index.php;
location = / { }
location / {
try_files $uri $uri/ /controller.php;
}
location /api {
try_files $uri $uri/ /api/controller.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
The first two lines are from your original configuration.
The location = will ensure that the URI / obeys the index index.php rather than the try_files rule next.
The location / defines the default action, serving image.png, index.php on subdirectories and controller.php on anything else.
The location /api modifies this behaviour with respect to controller.php.
The location ~ \.php$ block implements the fastcgi interface. Notice that include fastcgi_params; should come before any fastcgi_param directives to avoid the latter being silently overridden.
If you would like the URI domain.com/blah.php to call controller.php instead of throwing a 404, then change the =404 to /controller.php.
Please see this for a list of nginx directives.

CakePHP Htaccess 2 Nginx rewrite

We're moving a CakePHP Framework installation to a server where there's an Nginx running. Previous server had Apache. This CakePHP has multiple sub-installations on subfolders which all include the /app/webroot/ folder. We've managed to get the index.php working but all the other files located under /app/webroot/ like javascript and CSS don't link up there.
Now, we've tried getting this to work on nginx with multiple different variations. The problem is, the site loads up PHP files and clean URL'S work. Loading CSS and JS files which are located under /app/webroot/ don't.
We're trying to set up the root to subdomain.example.com where there's an index.php with a header() function to redirect the user to a folder, where there's CakePHP. Basically multiple sites under sub folders. So the CakePHP sites are http://subdomain.example.com/subfolder
Here's the nginx conf we're trying. I've been trying various different options with no effect.
server {
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com www.example.com subdomain.example.com;
access_log /home/example.com/logs/access.log;
error_log /home/example.com/logs/error.log error;
root /home/example.com/public_html/;
index index.php;
gzip_static on;
location /subfolder {
root /home/example.com/public_html/subfolder/;
index index.php;
rewrite ^/subfolder/(/.*)$ /app/webroot$1 break;
try_files $uri $uri/ /subfolder/app/webroot/index.php?$args;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_comles $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/example.com-php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
app/webroot/ will be your server root. And separate location for process index.php file.
Example:
server {
listen 80;
server_name yourserver.com;
root /web/path/;
index index.php;
location / {
rewrite ^(/.*)$ /app/webroot$1 break;
try_files $uri $uri/ /app/webroot/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}