Nginx redirect to PhantomJS - javascript

I am attempting to redirect to a phantomJS instance running on port 8888. However it is failing. The regular page loads, but when I change the #! for the ?_escaped_fragment_= it just gives me the regular page still...
Excerpt from the nginx file
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
if ($args ~ _escaped_fragment_) {
proxy_pass http://localhost:8000/?_escaped_fragment_=/;
}
#mi angular app
server {
location / {
root /var/www/html/miwebapp/client/app;
}
}
##
# Basic Settings
##
sendfile on;
tcp_nopush on;

The solution found at the bottom of this "crawlable application" post on senior-java-developer.com shows how to address the issue by rewriting to another location that handles the proxy_pass as follows:
if ($args ~ "_escaped_fragment_=(.*)") {
rewrite ^ /snapshot${uri};
}
location /snapshot {
proxy_pass http://api;
proxy_connect_timeout 60s;
}

My working configuration:
if ($args ~ _escaped_fragment_) {
rewrite ^ /snapshot$uri;
}
location ~ ^/snapshot(.*) {
rewrite ^/snapshot(.*)$ $1 break;
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_connect_timeout 60s;
}
More info here: https://github.com/liuwenchao/ajax-seo

Related

Livewire is not defined

I am having an issue deploying my Laravel Livewire project to the server. The issue is that livewire.js file is not accessible and showing an error in the console
Livewire is not defined
My server environment is LNMP. I am using Nginx instead of apache. There is an alternate way to fix this by publishing livewire which copies files from vendor folder to resources folder. By you have to maintain js files yourself after that and i don't want to do that.
It may be caused by Nginx by blocking js response files, How can I fix this
This is my nginx config file
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
#AAPANEL_FASTCGI_CONF_BEGIN
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_path /dev/shm/nginx-cache/wp levels=1:2 keys_zone=WORDPRESS:100m inactive=60m max_size=1g;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
#AAPANEL_FASTCGI_CONF_END
include mime.types;
#include luawaf.conf;
include proxy.conf;
default_type application/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server_tokens off;
access_log off;
server
{
listen 888;
server_name phpmyadmin;
index index.html index.htm index.php;
root /www/server/phpmyadmin;
location ~ /tmp/ {
return 403;
}
#error_page 404 /404.html;
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
access_log /www/wwwlogs/access.log;
}
include /www/server/panel/vhost/nginx/*.conf;
}
livewire.js does not change as much, unless there is a change in composer.json regarding laravel-livewire package, i suggest you just publish your livewire.js file using
php artisan livewire:publish --assets
And then adding auto publishing script
{
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi",
"#php artisan vendor:publish --force --tag=livewire:assets --ansi"
]
}

Nginx audio files (wav/ogg/mp3) not working

Audios on prod are not working while working fine on dev environment (Angular 7).
Prod config (VPS):
Ubuntu 18
Nginx
Let's encrypt
AudioService:
export class AudioService {
audio = new Audio();
constructor() { }
isPlaying() {
return this.audio.currentTime > 0 && !this.audio.paused && !this.audio.ended && this.audio.readyState > 2;
}
play(name: string): void {
this.audio.src = `assets/audio/${name}`;
this.audio.crossOrigin = 'anonymous';
this.audio.load();
if (!this.isPlaying()) {
this.audio.play();
}
}
pause(): void {
if (this.isPlaying()) {
this.audio.pause();
}
}
}
CORS are enabled on Nodejs side (using Nestjs). main.ts:
app.enableCors();
Chrome log:
Uncaught (in promise) DOMException: Failed to load because no
supported source was found.
Firefox log:
NotSupportedError: The media resource indicated by the src attribute
or assigned media provider object was not suitable.
Looking at Network console we can see myaudio.wav with:
Status Code: 206 Partial Content
Note: Loading images works fine !
EDIT:
Nginx config /etc/nginx/sites-available/mywebsite:
# Redirection
server {
# if ($host = mywebsite.com) {
# return 301 https://$host$request_uri;
# } # managed by Certbot
listen 80;
listen [::]:80;
server_name mywebsite.com www.mywebsite.com;
return 301 https://$host$request_uri;
#return 404; # managed by Certbot
}
# Config
server {
server_name mywebsite.com www.mywebsite.com;
root /home/foo/mywebsite/gui;
index index.html index.htm;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://my.ip:3000/;
# Websocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
if ($host = 'www.mywebsite.com') {
return 301 https://mywebsite.com$request_uri;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
}
On dev environment localhost:4200/assets/audio/myaudio.wav → works fine
On prod environment https://mywebsite.com/assets/audio/myaudio.wav → returns home page
While https://mywebsite.com/assets/image.jpg → works fine
Only audios don't work.
Set max_ranges to 0.
For your case, this would look like something like this:
location ~ \.wav$ {
max_ranges 0;
}
Meaning the rule applies to every wav file regardless of their location.

config ssl nginx server for prerender angular js app

Can you tell me how to config nginx to prerend my app.
The tutorial doesn't work, when I crawl the website with crawler googlebot, I see brackets from angular.
I'm lost.
many thanks in advance
my nginx config:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name www.thedomain.com;
ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/www.thedomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.thedomain.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location / {
# Proxy_pass configuration
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://0.0.0.0:3000;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
}
This should work:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name www.thedomain.com;
ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/www.thedomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.thedomain.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location / {
#proxy_set_header X-Prerender-Token YOUR_TOKEN;
set $prerender 0;
if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
set $prerender 1;
}
if ($args ~ "_escaped_fragment_") {
set $prerender 1;
}
if ($http_user_agent ~ "Prerender") {
set $prerender 0;
}
if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
set $prerender 0;
}
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
resolver 8.8.8.8;
if ($prerender = 1) {
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
set $prerender "service.prerender.io";
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://$prerender;
}
# Proxy_pass configuration
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://0.0.0.0:3000;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
}
Don't forget to remove the # from the proxy_set_header for X-Prerender-Token once you replace YOUR_TOKEN.
You will then want to test your URL be appending the ?_escaped_fragment_= query parameter. If your URLs look like:
https://www.example.com/
You'll test them like:
https://www.example.com/?_escaped_fragment_=
That will force a prerendered page so you can View Source there and make sure you're seeing a static HTML page instead of just the tags.

Nginx redirects static files to subdir of root

I have an app that runs at http://192.168.0.2:8080/. The index.html page is in /web folder and it requests static files (eg. css) at /css.
I'd like to use nginx as reverse proxy and to have myapp.mydomain.com to redirect to my app.
I have the following configuration in my nginx.conf:
server {
listen 80;
server_name myapp.mydomain.com;
satisfy any;
location / {
proxy_pass http://192.168.0.2:8080/web/;
index index.html index.htm;
}
}
but it does not work for css files since it looks for them at /web/css.
My workaround is to have my nginx.conf configured this way (without /web):
server {
listen 80;
server_name myapp.mydomain.com;
satisfy any;
location / {
proxy_pass http://192.168.0.2:8080/;
index index.html index.htm;
}
}
and request http://myapp.mydomain.com/web each time.
But I'd like to be able to request http://myapp.mydomain.com/ and let nginx manage.
I think something similar to that may help but I can't find how:
location ~ .(css|img|js)/(.+)$ {
try_files $uri /$uri /$1/$2;
}
location / {
proxy_pass http://192.168.0.2:8080/web/;
index index.html index.htm;
}
Here is my complete file, with auth etc:
upstream app { server 192.168.0.2:8080; }
server {
listen 80;
server_name myapp.mydomain.com myapp.myOtherDomain.com;
satisfy any;
allow 192.168.0.0/24;
deny all;
auth_basic "closed site";
auth_basic_user_file /etc/nginx/auth/passwords;
location / { proxy_pass http://app/web/; }
location /css { proxy_pass http://app; }
location /img { proxy_pass http://app; }
location /js { proxy_pass http://app; }
}
Any idea how I can fix that?
Thanks.
From what I understand, you have a working configuration and the only problem is that you would like the URL http://myapp.mydomain.com/ to be mapped to http://192.168.0.2:8080/web/.
Your working configuration is:
server {
listen 80;
server_name myapp.mydomain.com;
satisfy any;
location / {
proxy_pass http://192.168.0.2:8080/;
index index.html index.htm;
}
}
The simplest solution is to add an exact match for the / URI. Such as:
server {
listen 80;
server_name myapp.mydomain.com;
satisfy any;
location = / { rewrite ^ /web/; }
location / {
proxy_pass http://192.168.0.2:8080/;
index index.html index.htm;
}
}

Redirect to https not working (and https itself not working)

My goals are to get https to work, and then get the http to https redirections, and the non-www to www redirections to work.
Nginx.conf looks like:
http {
include mime.types;
default_type application/octet-stream;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
include site.conf;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
}
site.conf looks like:
upstream site_local {
server 127.0.0.1:9090 max_fails=3 fail_timeout=20s;
}
server {
listen 80;
server_name site.com;
return 301 https://www.site.com$request_uri;
}
server {
listen 80;
server_name www.site.com;
return 301 https://www.site.com$request_uri;
}
server {
listen 443;
server_name www.site.com;
set $app_root /home/site/site-web-node/public;
ssl on;
ssl_certificate /etc/ssl/SSL.crt;
ssl_certificate_key /etc/ssl/site.key;
location / {
proxy_pass https://site_local;
client_max_body_size 10m;
client_body_buffer_size 128k;
}
location ~ /(app|lang|js) {
root $app_root;
}
}
service nginx reload is successful, however the website's https isn't working, nor the redirects. http is working as before.
nginx -t returns successful.
I am getting the same error
Its show me like this when i run sudo netstat -tlnp | grep 443
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 10031/nginx
Then i found it was the site available server config file was the problem. Its working now.

Categories

Resources