I'm trying to serve a React app build in nginx in /opt/hdr/static/hdr. React homepage and Router are pointing /static/hdr as React documentation says.
The .conf file I'm using is the next one:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
index index.html;
location /static/hdr/ {
alias /opt/hdr/static/hdr/;
index index.html;
expires 1d;
add_header Cache-Control public;
}
}
But I'm getting a 404 error when I access mywebpage.com/static/hdr. I've tried several combinations:
alias /opt/hdr/static/hdr
root /opt/hdr
root /opt/hdr/static/hdr
Nothing works. The thing is, looking in the error logs of nginx I've realized that it is searching in /etc/nginx/html/static/hdr instead of /opt/hdr/static/hdr. If I put the site there everything works perfectly.
Executing nginx -V gives me that --prefix is set to /etc/nginx. This could be the reason why nginx searches in /etc/nginx but I have no idea where /html is coming from. Neither why root and alias are not overwriting it.
Any idea is welcome. Thanks in advance.
Related
So I've got this NGINX config
server{
listen 80 default_server;
listen [::]:80 default_server;
server_name cerdman.me;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443;
server_name cerdman.me;
ssl_certificate /etc/letsencrypt/live/cerdman.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/cerdman.me/privkey.pem;
root /home/ubuntu/portfolio/resume/; #show users the resume by default
index index.html;
# Other config you desire (TLS, logging, etc)...
location /resume/ {
root /home/ubuntu/portfolio;
index index.html;
}
location /weatherapp/ {
root /home/ubuntu/portfolio;
index index.html;
}
}
What I'm trying to achieve here is to have my resume be served by default if someone goes to https://cerdman.me. I'm having trouble doing this in a way that doesn't interfere with my other project(s).
My second project, at /weatherapp/ was created using create-react-app. As is, my app will try to access resources from several urls, lets take the example https://cerdman.me/static/js/2.70e4302a.chunk.js. When this url request is sent to my server, the server doesn't have a location block for it, so it tries to use the server's root directory. In the end it looks in /home/ubuntu/portfolio/resume/static/js/2.70e4302a.chunk.js instead of /home/ubuntu/portfolio/weatherapp/static/js/2.70e4302a.chunk.js where the files actually are.
What I need to do is somehow to redirect these resource requests being made by the weatherapp project such that I can maintain my current file structure, or to find an alternative structure that circumvents the problem entirely while still being scalable. Maybe putting both projects into one server isn't the right way?
I just installed ngninx on my dev machine.
It automatically migrated my vhosts from lighttpd (very comfy!), I only had to adjust the TLDs (it only took \.dev, I changed that to \.(dev|test|local).
and bound itself to port 81; after removing lighttpd, I changed the ports in /etc/nginx/sites-available to 80.
But when I call http://<ip-adress>/ in the browser, I get the index page of one of my vhosts instead of the default DOCUMENT_ROOT (/var/www/).
I touched /etc/nginx/sites-available/default, changed the port number and uncommented the PHP block.
current contents (comments stripped):
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
index.php index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
Half of the vhosts had self-references in /etc/nginx/sites-enabled, I replaced them with symlinks to /etc/nginx/sites-available and added a symlink for default; all my vhosts can now be accessed, but calling the IP address still routes to the same vhost instead of /var/www.
That vhost file is neither alphabetically first nor considering the mtime, but it is when I list the directory unsorted (ls -f), it even comes before ...
How do I get nginx to deliver /var/www/ instead of /var/www/vhost/?
update: After a few clicks on my primary vhost, switching to https and back, it changed:
http://www.vhost1.test now routes to /var/www, but the other vhosts seem to work correctly.
update: I tried to solve the problem by uncommenting the server block in nginx.conv (pointing to /var/www) and linking sites-enabled/default to sites-available/vhost1. The latter resulted in both the ip-address and vhost1 getting routed to another vhost. The other vhosts are still working fine.
I got it:
sites-available/vhost1 only had listen 443 ssl;; listen 80; was missing
(because listen 80 default_server caused a "duplicate default server" error),
so calling the domain via port 80 fell back to the default server.
I installed nginx using sudo apt-get install nginx.
Now this allows me to go to my_ip:port and it allows me to visit the website.
Yet, i can also do my_url:port and it will also direct me to the website.
How can nginx know my_url when I have not told it my_url anymore?
I was running Apache before, can that explain it?
Nginx was able to load via the fqdn my_url:port even though you haven't added my_url in the nginx config because config default_server (usually there by default) was specified.
default_server parameter specifies which block should serve a request if the server_name requested does not match any of the available server blocks:
For example
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
Nginx doesn't need it (at least, not yet). Your web browser looks up my_url in the DNS, and then uses my_ip (from DNS) :port (which you entered in your browser) to connect to Nginx.
Your Nginx is probably only configured with one site, which means any connection to it - regardless of whether it is by IP or by domain name - causes Nginx to serve that site. You can change this by going into your Nginx configuration files and setting (or changing) the value of the server_name parameter, for example:
server { # You already have a server block somewhere in the config file
listen 80; # Or 443, if you've enabled SSL
server_name example.com www.example.com; # Add (or change) this line to the list of addresses you want to answer to
I am trying to serve a website with nginx. I have noticed that when I make changes to my /etc/nginx/sites-available/game, run sudo service nginx restart, it is not reflected when I try to pull it up in the browser.
The browser just hangs and waits for a response and then timesout.
However, it works perfectly fine if I try to do a curl request to my site on the command line. I get the normal nginx html basic file. Why is that? Here. (and yes, I have made a soft link from sites-enabled/game to sites-available/game)
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name my.site.uw.edu;
location / {
try_files $uri $uri/ =404;
}
}
Also, I am using Ubuntu 14.04. I don't think this version of Linux uses SELinux, but could this be some sort of security configuration related deal? I have had trouble in the past with SELinux when deploying on CentOS machines.
You can disable adding or modifying of “Expires” and “Cache-Control” response header using expires param:
expires off;
nginx docs
I am reading nginx beginner's tutorial, on the section Serving Static Content they have
http {
server {
}
}
but when I add an http block I get the error
[emerg] "http" directive is not allowed here …
When I remove the http block and change the conf file to this, it works fine:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/example.com/html;
index index.html index.htm;
# make site accessible from http://localhost/
server_name localhost
location / {
try_files $uri $uri/ /index.html;
}
I suspect that I am missing something simple, but why do they use http to serve static files?
Your doing fine. I guess you are editing /etc/nginx/sites-enabled/default (or the linked file at /etc/nginx/sites-available/default.
This is the standard nginx set up. It is configured with /etc/nginx/nginx.conf which contains the http {} statement. This in turn contains an "include /etc/nginx/sites-enabled/*" line to include your file above with server{ } clause in it.
Note that if you are using an editor that creates a backup file, you must modify the include statement to exclude the backup files, or you will get some "interesting" errors! My line is
include /etc/nginx/sites-enabled/*[a-zA-Z]
which will not pick up backup files ending in a tilde. YMMV.