How to set nginx location - nginx

here is my question
I am learning nginx recently
And I don't know why I can handle my config about location
here is my nginx.conf
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root /Users/billyliou/web-site;
location / {
root html;
index index.html;
}
location /demo {
index index.html index.htm;
try_files $uri $uri/ /demo/index.html?s=$uri&$args;
}
location /test {
index index.html index.htm;
try_files $uri $uri/ /test/index.html?s=$uri&$args;
}
...
I put two folders into '/Users/billyliou/web-site'
one is demo , the other is test.
Both contain index.html.
And when I type localhost:8080, it shows nginx default page.
When I type localhost:8080/demo it shows demo page.
but when I type localhost:8080/test , it shows demo page too.
the last one is beyond my expectation.
How can I set this config?

Try this
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /demo {
root /Users/billyliou/web-site/demo;
index index.html index.htm;
try_files $uri $uri/ /index.html?s=$uri&$args;
}
location /test {
root /Users/billyliou/web-site/test;
index index.html index.htm;
try_files $uri $uri/ /index.html?s=$uri&$args;
}
location / {
root /Users/billyliou/web-site;
index index.html;
}
...
}

Related

Nginx removing parameters from request in main page

Good day,
Please tell me how to remove the parameters of the GET request from the main page of the site and so that the rest of the parameters are processed. That is, if there are any parameters on the main page when requested, make a redirect to the site without parameters.
server {
listen 80;
listen [::]:80;
root /var/www/public_html/public;
index index.php index.html index.htm;
server_name mysite.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
Not tested but somethings on below lines should help you out
server {
listen 80;
listen [::]:80;
root /var/www/public_html/public;
index index.php index.html index.htm;
server_name mysite.com;
location / {
location = / {
if ($is_args)
rewrite / permanent;
try_files $uri $uri/ /index.php?$query_string;
}
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
#include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}

Content linked to domain is not working but with subdomain it is working on nginx

I have created several subdomain so that i can serve the subdomain related specific content. But the problem i am facing is that when i am trying to access the content related directly to my domain am not getting anything in return in nginx. Below is my configuration file
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name example.com www.example.com;
root /usr/share/nginx/myfile/dev/web;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name dev.example.com;
root /usr/share/nginx/myfile/dev/mobile;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name qa-crm.example.com;
root /usr/share/nginx/myfile/qa-crm;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name qa.example.com;
root /usr/share/nginx/myfile/qa;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
}
All server block are serving perfectly except the first one which contains the main domain name i.e. (example.com www.example.com)
No idea what i am doing wrong here. I am new to nginx world

nginx config add location lead error

I met a problem about Nginx really make me confused.
if I set the server as blow :
server {
listen 80;
server_name _;
root /usr/project/exchange/front/build;
index index.html;
location = /{
try_files $uri $uri/ =404;
}
}
if I add location / in the server. the page shows error.
server {
listen 80;
server_name _;
root /usr/project/exchange/front/build;
index index.html;
location = /{
try_files $uri $uri/ =404;
}
location / {
proxy_pass http://127.0.0.1:8088;
}
}
That's really make confused. Nginx match the url from =/ to /?Why add location / leads to mistake?

Config nginx for Vue.js distribute project

I use the nginx for Vue.js dist server.
my Nginx config is bellow:
server {
listen 80;
server_name qy.abc.xyz;
charset utf-8;
access_log /var/log/nginx/qy.abc.xyz.access.log main;
location / {
access_log /data/nginx_log/access.log main;
error_log /data/nginx_log/error.log error;
root /data/ldl/repo/vue_user_site/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
...
I can access the qy.abc.xyz in browser success, but I can you know in my Vue.js there are many routes, if I access qy.abc.xyz/index or qy.abc.xyz/xxx/xxx, Nginx will get the 404 Not Found error.
You know the dist directory is constituted by many hashed-name.js and a index.html.
How to config my Nginx for my project?
EDIT-1
I tried use this config, but not work.
location / {
access_log /data/nginx_log/access.log main;
error_log /data/nginx_log/error.log error;
#root /data/ldl/repo/vue_user_site/dist;
#index index.html;
#try_files $uri $uri/ /index.html;
return 200 /index.html;
}
I need to configurate URL Rewrite
you can try this
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root <your static file root>
}
location / {
return 200 /index.html
# or use rewrite
rewrite ^.*$ /index.html
}
Or use some server code that has route like node , asp.net core to send your html
Follow my settings of single-page application config in Nginx:
server {
listen 80;
server_name qy.abc.xyz;
#access_log /var/log/logs/qy.abc.xyz.access.log main;
charset utf-8;
index index.html;
root /data/ldl/repo/vue_user_site/dist;
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}

vue with nginx: path not matching

I am setting up vue(2.1.x) with nginx(1.10.2), I have following configuration:
location / {
root /var/lib/myRepo/dist/;
index index.html;
}
This works when I hit 'http://localhost:8080/', but when I hit other URLs like: 'http://localhost:8080/products/home', I get:
404 Not Found
I also tried following:
root /var/lib/myRepo/dist/;
location / {
try_files $uri $uri/ index.html$query_string;
}
What can be wrong here and how to correct this.
you can follow like this:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
try_files $uri $uri/ #router;
index index.html;
}
location #router {
rewrite ^.*$ /index.html last;
}
}

Resources