ngnix issue with chrome redirection non-www to www - linux

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.

Related

Nginx v1.14.2 -- too many redirects

In my limited experience with Nginx, I get too many redirects when I deploy many WEB projects on the one domain.
I want to use one 'location' to match multiple items.
File path: /mnt/vue/web and /mnt/vue/admin and /mnt/page/web
When I visit https://${domain}/page/single, it is wrong. It redirects to https://${domain}/page/single/index.html/index.html/index.html/...
When I visit https://${domain}/vue/web, it is ok.
I've tried multiple variations of this, but none of them seem to work. Any ideas? Thanks in advance.
Here is my Nginx conf:
server {
listen 443 http2;
server_name ${domain};
ssl on;
ssl_certificate /etc/nginx/conf.d/cert/web/web.pem;
ssl_certificate_key /etc/nginx/conf.d/cert/web/web.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# wechat verify
root /mnt/wechat;
# for vue h5-wechat
location ~ /(.*)/web {
root /mnt;
try_files $uri $uri/ /$1/web/index.html =404;
}
# for vue admin-web
location ~ /(.*)/admin {
root /mnt;
try_files $uri $uri/ /$1/admin/index.html =404;
}
# for api
location ~ /(.*)/api/(.*)$ {
proxy_pass http://172.16.184.254:$1;
rewrite /(.*)/api/(.*)$ /$2 break;
}
# for single pages !!!!! Where the problem occurred.
location ~ /(.*)/single/ {
alias /mnt/$1/web/;
index index.html;
}
# for cache
location ~ /(css|img|js|fonts|image)/ {
add_header Cache-Control "public, max-age=300";
}
location /files {
rewrite /files/(.*)$ /$1 break;
proxy_pass https://172.16.92.152;
}
}
server {
listen 80;
server_name ${domain};
return 301 https://$server_name$request_uri;
#rewrite ^(.*)$ https://$host$1 permanent;
}
Using alias within a regular expression location block, you need to capture the entire URI. See this document for details.
For example:
location ~ ^/(.*)/single/(.*)$ {
alias /mnt/$1/web/$2;
index index.html;
}

Nginx allow user agent specific directory

I migrate apache to nginx.
In apache I put .htaccess inside folder, but nginx hasn't this.
I have folder /admin and it can only by accessed by User Agent: Administrator
I do:
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
And it works, but .php files isn't interpreted.
How can I do this?
Edit:
server {
listen 80;
server_name myserver.com;
root /usr/share/nginx/html;
location / {
}
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
include /etc/nginx/default.d/*.conf;
}
There is problem with the specification about agent callback instead of "Administrator" it should be (Administrator) rest of the code seems to be ok. If you have added try_files $uri =404; additionally then remove it as it don't required. return 404 already defined.
Try this one
server {
listen 80;
server_name myserver.com;
root /usr/share/nginx/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
}
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
include /etc/nginx/default.d/*.conf;
}
restart nginx service before test!

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

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/;
}
}

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