Getting error 'XMLHttpRequest cannot load /socket.io' in case of socket.io - node.js

I am getting error:"XMLHttpRequest cannot load /socket.io " for socket call. what's wrong there,i am not getting it.i am new to web sockets,my other api call is running good,but socket.io call is not working. i have installed ssl certificate for nginx,help me guys.
My nginx configuration is
server{
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
server {
#listen 80;
listen 443 ssl;
# Make site accessible from http://localhost/
server_name www.example.com;
ssl_certificate "/home/oodles4/sslcertificate/public.crt";
ssl_certificate_key "/home/oodles4/sslcertificate/keyfile.key";
proxy_set_header X-Forwarded-For $remote_addr;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
#location / {
root "/home/oodles4/Documents/dcx/user/dev";
#root "/home/oodles4/Documents/dcx/explorer";
index home.html home.htm;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# }
location /{
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
9,51 2%
#location / {
root "/home/oodles4/Documents/dcx/user/dev";
#root "/home/oodles4/Documents/dcx/explorer";
index home.html home.htm;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# }
location /{
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
}
location /admin{
alias "/home/oodles4/Documents/dcx/admin/dev";
index home.html home.htm;
}
location /api {
proxy_pass http://localhost:8080/api;
}
location /admin/api{
proxy_pass http://localhost:8080/api;
}
location /explorer{
alias "/home/oodles4/Documents/dcx/explorer";
# try_files $uri $uri/ /views/main/main.html;
index dev/views/main/main.html;
}
location /explorer/api{
proxy_pass http://localhost:3000/api;
}
when i do '/explorer', my all api's call and socket will work
var socket = io.connect('http://www.example.com:3000', {
'reconnect': true,
'reconnection delay': 500
});

I guess you have server blocks to listen on ports 80, 443.
but you do not have any server block to listen on 3000.
Here i assume that you have any application running on port 3000.
Just append this block to current nginx configuration.
server {
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000/;
}
}

Related

ngnix issue with chrome redirection non-www to www

I have done basic configuration for my site to run using nginx,
server {
listen 80;
root /home/htdocs/;
index index.html index.htm index.php;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ /index.php?$args /;
}
# added error page
error_page 404 = #notfound;
location #notfound {
return 301 /;
}
location ~ \.php$ {
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 443 ssl http2;
#listen [::]:443 ssl;
# SSL
ssl_certificate /etc/letsencrypt/live/example.com-0002/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com-0002/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com-0002/chain.pem;
ssl_protocols TLSv1.2 TLSv1.3;
root /home/htdocs/;
index index.html index.htm index.php;
server_name www.example.com;
#return 301 https://$server_name$request_uri;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ /index.php?$args /;
}
# added error page
error_page 404 = #notfound;
location #notfound {
return 301 /;
}
location ~ \.php$ {
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Whats wrong with above config even I tried in incognito mode of google-chrome but still same.
Hope to hear from you with solution if I'm doing any thing wrong.
Regards
I have tried different solutions and parameters noting work out for me.

how to check nginx is serving static file or not

This is my nginx.conf file. i want to serve all static contents that are located in different directories.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
access_log C:/Program Files/nginx-1.11.7/logs/access.log;
}
location ~* \.(css|js|gif|jpe?g|png|html)$ {
root D:/gamma-master;
autoindex on;
access_log C:/Program Files/nginx-1.11.7/logs/access.log;
}
}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
How to check nginx is serving static file or not. Also how should give static file path if my static content is located in different folders under root folder

nginx GET .php variables

I have a bunch of PHP files that take .php?id=123 and I need to get them all. How do I do them all in my config file?
I can't seem to figure out how to make use of
get1.php?id=stuff
get2.php?id=stuff
get3.php?id=stuff
and so on...
The problem is how do I do that when they are all under the same root directory?
With the following I get 500 ERROR on the p.php?id=945 but PHP works fine but I CANT login or get POST data to work
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
location / {
index index.php index.html index.htm;
location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
expires 1d;
try_files $uri?$args #backend;
}
error_page 405 = #backend;
add_header X-Cache "HIT from Backend";
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #backend {
internal;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(php|jsp|cgi|pl|py)?$ {
try_files $uri?$args /index.php;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
THIS: just 500s
rewrite or internal redirection cycle while internally redirecting to "/index.php"
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
#try and serve static files directly
location ~* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
try_files $uri #inPlaceDynamicFile;
expires 24h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
#allow us to have dynamic css/js files
location #inPlaceDynamicFile {
# examples /css/cssIncludes.css => /css/cssIncludes.css.php
try_files $uri.php =404;
fastcgi_pass 127.0.0.1:9001;
include fastcgi_params.conf;
}
location / {
try_files $uri?$args /index.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
}
"Is this correct?"
Nope.
1) Root should never be in a location block, it should only be in a server block.
2) Apache rewrite for testing if something is an existing file is done with try_files not with Nginx rewrite. See also
3) Your proxy_pass is going to be passing through to the same server which has got to be a circular redirect.
4) You've also got a location block inside a location block, which seems quite odd. Although that may be needed for some advanced config, you don't need it for what you're doing.
I think you probably want you nginx conf to be like this:
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
#try and serve static files directly
location ~* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
try_files $uri #inPlaceDynamicFile;
expires 24h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
#allow us to have dynamic css/js files
location #inPlaceDynamicFile {
# examples /css/cssIncludes.css => /css/cssIncludes.css.php
try_files $uri.php =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params.conf;
}
location / {
try_files $uri /p.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
}
If you do want the only requests that contain .php to go to your p.php file then you should replace the last location block with:
location ~ /(.*)\.php {
try_files $uri /p.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
location / {
try_files /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
Edit
Do I need to create specific rewrites for EVERY php file that has a
$_GET[''];?
No - you should be able to pass them in to any file with the try_files like:
location / {
try_files $uri?$args /index.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
That would match any .php request to a php file if it exists with the query string appended , and then fall back to index.php if the .php file doesn't exist with both the query string and path passed in.

Nginx Restart Warnings after WWW rewrite

I am trying to make a rewrite for my website to have eosgaming.com rewrite to www.eosgaming.com.
Here is my virtual.conf
#
# A virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
server {
listen 80;
server_name eosgaming.com;
rewrite ^/(.*) http://www.eosgaming.com/$1 permanent;
}
server {
listen 80;
server_name .eosgaming.com;
error_log /home/web/eosgaming.com/logs/error.log;
root /home/web/eosgaming.com/public_html/;
index index.html index.htm index.php;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name .forum.eosgaming.com;
error_log /home/web/forums.eosgaming.com/logs/error.log;
root /home/web/forums.eosgaming.com/public_html/;
index index.html index.htm index.php;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name .mysql.eosgaming.com;
error_log /home/web/mysql.eosgaming.com/logs/error.log;
root /usr/share/phpMyAdmin;
location / {
index index.php;
}
## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
expires 360d;
}
location ~ /\.ht {
deny all;
}
location ~ /(libraries|setup/frames|setup/libs) {
deny all;
return 404;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/phpMyAdmin$fastcgi_script_name;
}
}
server {
listen 80;
server_name .source.eosgaming.com;
error_log /home/web/source.eosgaming.com/logs/error.log;
root /home/web/source.eosgaming.com/public_html/;
index index.html index.htm index.php;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
And here is the errors I am getting
[root#vd1 ~]# service nginx restart
nginx: [warn] conflicting server name "eosgaming.com" on 0.0.0.0:80, ignored
Stopping nginx: [ OK ]
Starting nginx: nginx: [warn] conflicting server name "eosgaming.com" on 0.0.0.0:80, ignored
How can I fix this issue? I just want to have my site add www.
I would recommend combining your first two server directives into one like:
server {
listen 80;
server_name eosgaming.com; #removed the . because its not needed
if ($host ~* ^[^.]+\.[^.]+$) { #check if the host has no subdomain
rewrite ^(.*)$ http://www.$host$1 permanent; #Rewrite the url to include www.
}
error_log /home/web/eosgaming.com/logs/error.log;
root /home/web/eosgaming.com/public_html/;
index index.html index.htm index.php;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
As .eosgaming.com; is equivalent to eosgaming.com *.eosgaming.com;, it will conflict with server_name eosgaming.com;.

FastCGI - Rewrite - Location with Nginx

I just switched to nginx but I'm having trouble with my URL-Rewriting
I used
location /id/ {
rewrite ^/id/(.*) /index.php?id=$1 break;
}
But the php code is not interpreted, worst it's downloaded raw.
Yet .php files are configured as follow:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/my_app$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
What is wrong with my vhost ?
EDIT: Here is the entire vhost
server {
listen 80; ## listen for ipv4
server_name viditx.com www.viditx.com; ## change this to your own domain name
# I find it really useful for each domain & subdomain to have
# its own error and access log
error_log /var/log/nginx/viditx.com.error.log;
access_log /var/log/nginx/viditx.com.access.log;
root /var/www/viditx;
location / {
# Change this to the folder where you want to store your website
index index.html index.htm index.php;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# again, change the directory here to your website's root directory
# make sure to leave $fastcgi_script_name; on the end!
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Did you read the documentation?
break -> last:
location /id/ {
rewrite ^/id/(.*)$ /index.php?id=$1 last;
}
You should try :
location /id/ {
rewrite '^/id/(.*)$' /index.php?id=$1 break;
}
and :
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
Have fun !

Resources