I'm trying to implement universal links from my iOS app, thus I need to have the "apple-app-site-association" file at the root of my web server.
I am using nginx and everything is (supposedly) setup correctly. My website works, php works, my api works. The only problem is that when I go to my site "test.com/apple-app-site-association" the browser (as well as the iOS browser) downloads the file instead of just displaying it, thus making my universal link not work.
If anyone has any ideas on how to stop nginx from offering the site as a download and serving it instead I would be glad.
Below is my server's configuration, with my site edited out:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name test.com www.test.com;
return 301 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-test.com.conf;
include snippets/ssl-params.conf;
client_max_body_size 1m;
root /var/www/test.com/web;
index index.html index.htm index.php;
access_log /var/log/nginx/test.com.access.log;
error_log /var/log/nginx/test.com.error.log;
#Default url rewrites
location / {
#root /var/www/test.com/web;
try_files $uri $uri/ /index.html;
autoindex off;
index index.html index.htm index.php;
}
location ~ \.php$ {
# try_files $uri =404;
set $path_info $fastcgi_path_info;
root /var/www/test.com/web;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
try_files $uri $uri/ /index.php$is_args$args;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APP_ENV dev;
}
#Apple universal links
location = /apple-app-site-association {
default_type application/pkcs7-mime;
}
#Redirect all requests under /user/ to the "get App" page
location /user/ {
return 301 https://test.com/get_app.html;
# try_files $uri $uri/ /get_app.html;
}
#Let's Encrypt SSL Validation
include snippets/letsencrypt.conf;
}
Using a REST API client, requesting the file gives the following headers, that appear to be correct (no "attachment" header):
Server: nginx/1.10.3
Date: Sun, 02 Apr 2017 17:25:42 GMT
Content-Type: application/pkcs7-mime
Content-Length: 149
Last-Modified: Sun, 02 Apr 2017 16:07:02 GMT
Connection: keep-alive
Etag: "58e121a6-95"
Strict-Transport-Security: max-age=63072000; includeSubdomains
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Accept-Ranges: bytes
Your browser as well as the ios browser may not support the Content-Type: application/pkcs7-mime. If the content is human readable try setting Content-Type: text/plain.
Also see if this link is of any help.
Related
I install nginx 1.13.10 with a wordpress site and openssl on my virtual machine. I'm trying to test http2 push. Here is my nginx conf file:
pastebin.com/71ziXeRh
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 192.168.133.21;
return 302 https://$server_name$request_uri;
}
# Load configuration files for the default server block.
server {
listen 443 ssl http2;
server_name 192.168.133.21;
include conf.d/self-signed.conf; #ssl config
ssl on;
location / {
root /var/www/wordpress/current;
index index.php index.html index.htm;
http2_push /wp-content/themes/twentyseventeen/assets/images/header.jpg;
http2_push /wp-content/themes/twentyseventeen/style.css?ver=4.7.4;
}
error_page 404 /404.html;
location = /40x.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/wordpress/current;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Restart nginx and no error. But when I debug it with Chrome no resource was pushed demo image
Please tell me if I do something wrong somewhere.
It's a restriction in Chrome. If your SSL/TLS cert is not trusted (as we can see yours is not in the screenshot) it ignores pushed resources. Even if you skip paste the HTTPS error. Similarly resources on such a site cannot be cached.
Add the certificate to your browser's trust store so you get a green padlock and it should start to work.
Similar question (but using Node rather than Nginx) here.
I have wordpress working well in mysite.com
but YOURLS which is installed in mysite.com/u is not working, when I click on any shortened link I get a 404 error (wordpress).
However, I get YOURLS to work by adding this to nginx.conf
location /u { try_files $uri $uri/ /u/yourls-loader.php;
But then WordPress links break.
Here is my default nginx.conf
I know the fix is to add this try_files $uri $uri/ /u/yourls-loader.php; somewhere in nginx.conf , but where to put it without breaking wordpress.?
=================== Update 1 =========================
I got this partially working. with same config, but I noticed that wordpress links that start with u doesn't work ex: http://example.com/understand-math instead it redirect to Error 403 - Forbidden
???
================ update 2 ============
ok I fixed it by just adding another slash / to location /u/ instead of location /u
YOURLs NGINX CONFIGURATION
server {
# Listen IPv4 & v6
listen 80;
listen [::]:80;
# Optional SSL stuff
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate example.com.crt;
ssl_certificate_key example.com.key;
# Server names
server_name example.com www.example.com;
# Root directory (NEEDS CONFIGURATION)
root /path/to/files;
# Rewrites
location / {
# Try files, then folders, then yourls-loader.php
# --- The most important line ---
try_files $uri $uri/ /yourls-loader.php;
# PHP engine
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock; # Can be different
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
GITHUB DOCUMENTATION
I just installed a fresh server with Nginx, to run my client wordpress blog. The blog was migrated from an old server with Apache.
It's almost everything OK, but I'm getting 404 errors with image names that contain special characters.
For example, the link /wp-content/uploads/2014/06/Workshop-de-Tricô-e-Crochê-Círculo.png results in a 404 error.
Checking the server inputs, this URL becomes /wp-content/uploads/2014/06/Workshop-de-Tric%C3%B4-e-Croch%C3%AA-C%C3%ADrculo.png, and because of that results in a 404 error.
My server configuration is this:
server {
listen 80;
server_name blog.mysite.com;
root /home/forge/blog.mysite.com;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
access_log off;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Is there anything I can do?
The problem was:
My Mac (I don't know how), changed the files charset when I copy the files to the new server.
When I made the same process in a Ubuntu machine, worked very well. Thank's for the answers.
Hey i tested your nginx scenario without wordpress using my local installation and i dont have any issues with the file name. Could you double check the file location? You could try to create a simple setup without wordpress to debug your situation, and did you look at your access.log file?
Enclosed is my testsetup.
The file Workshop-de-Tricô-e-Crochê-Círculo.png is stored directly in the path /data/nginx-test
1,4M 18 Jul 22:33 Workshop-de-Tricô-e-Crochê-Círculo.png
nginx configuration:
server {
listen 80;
listen 443 ssl;
server_name test.*;
root /data/nginx-test;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 900;
}
}
request/response headers:
Remote Address:192.168.33.10:80
Request URL:http://test.vg/Workshop-de-Tric%C3%B4-e-Croch%C3%AA-C%C3%ADrculo.png
Request Method:GET
Status Code:200 OK
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Host:test.vg
Pragma:no-cache
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Response Headers
Accept-Ranges:bytes
Connection:keep-alive
Content-Length:1486529
Content-Type:image/png
Date:Fri, 18 Jul 2014 20:36:54 GMT
ETag:"53c98490-16aec1"
Last-Modified:Fri, 18 Jul 2014 20:33:20 GMT
Server:nginx/1.6.0
I moved from the setup of Apache 2 + Varnish to Nginx alone, and I'm kinda stuck with how I should setup/use ESI as well as fastcgi_cache in this setup.
First of all, the idea of ESI was that we setup a reverse proxy layer in front of the server to cache the cache-able parts of a page, then using esi to retrieve the dynamic parts. In my previous setup Varnish was acting as the reverse proxy and Apache only handles the esi requests when necessary.
My question is that now with Nginx acting as the sole server here, how do I make it to work? Do I need to setup another Nginx instance running as a reverse proxy server or something? I couldn't find any document on this.
The second question is regarding fastcgi_cache. I have set it up as described below but the cache does't seem to work for me, no cache file populated and I always get "MISS". I wonder if it's because I need to set max-age/shared-max-age in each controller for each to work?
fastcgi_cache_path /run levels=1:2 keys_zone=www_mysite_com:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/mysite.com/w/w/w/www/web;
index index.php index.html index.htm;
# Make site accessible from http://www.mysite.com
server_name www.mysite.com;
# Specify a character set
charset utf-8;
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
# h5bp nginx configs
# include conf/h5bp.conf;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Deny access to .htaccess
location ~ /\.ht {
deny all;
}
# Don't log robots.txt or favicon.ico files
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { access_log off; log_not_found off; }
# 404 errors handled by our application, for instance Symfony
error_page 404 /app.php;
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|backend/app|backend/app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME web/$fastcgi_script_name;
fastcgi_param HTTPS off;
fastcgi_cache www_mysite_com;
fastcgi_cache_valid 200 60m;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
}
By default, responses from the Symfony 2 application have a cache control header that disables caching:
Cache-Control: no-cache
If you would like nginx to cache pages you will have to change those headers.
You can find general information about caching in the documentation
The simplest solution is to use the SymfonyFrameworkExtraBundle (you already have it if you use the SF2 standard edition) and use annotations on your controllers and/or actions to specify the cache headers. You can find more info about this approach it the docs for the #Cache annotation.
I have a MediaTemple server from which I serve many websites. I use nginx and have the follow config file. I am correctly forwarding all non-www traffic (ie, http://example.com) to the appropriate directory. However, all the www traffic is returning 404 because my config file is looking for /directory-structure/www.sitename.com instead of /directory-structure/sitename.com
How can I have both www and non-www requests go to one directory? Thanks.
server {
listen 80;
server_name _;
root /var/www/vhosts/$host/httpdocs/;
error_page 404 /;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
#fastcgi_pass php;
fastcgi_pass 127.0.0.1:9000;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
# this prevents hidden files (beginning with a period) from being served
location ~ /\. { access_log off; log_not_found off; deny all; }
}
Starting with version 0.7.40 Nginx accepts regular expressions in server_name and captures. Thus it's possible to extract a domain name (without www) and use this variable in root directive:
server_name ~^(?:www\.)?(.+)$ ;
root /var/www/vhosts/$1/httpdocs;
Starting with 0.8.25 it is possible to use named captures:
server_name ~^(?:www\.)?(?P<domain>.+)$ ;
root /var/www/vhosts/$domain/httpdocs;
Another syntax to define named captures is (?<domain>.+) (PCRE version 7.0 and later). More on PCRE versions here
Try this and add the following in the above server config:
if ($host = "www.example.com") {
rewrite (.*) http://example.org$1;
}
What happens here, we are instructin nginx to serve the pages as http://example.com even though the browser URL reads http://www.example.com - I hope this works.
UPDATE
Try this for a generic version:
if ($host ~* "www.(.*)") {
rewrite ^ http://$1$request_uri?;
}
Given the potential issues with if as linked to in RakeshS's answer's comments, as well as the fact that RakashS's answer didn't work for me anyway, here's a solution that should be safer and worked for me with Nginx 1.0.14.
Add an additional server entry for each one of your server sections that does a rewrite:
server {
server_name www.yourwebsite.com;
rewrite ^ $scheme://yourwebsite.com$request_uri permanent;
}