I need to do 2 things, first set the expiration header to 30d and second to enable the page speed module. Non of them work so far, this is my nginx.conf file
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
include /etc/nginx/conf.d/*.conf;
}
For turning on pagespeed, you first need to build your nginx from source with pagespeed's module.
Its very easy! You could just follow Google's instruction here and then here
After you complied Nginx from source with pagespeed module enabled, you could just add this to your conf:
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
To set the expiration header, I think it's better to put your code inside a server block, and then inside the main location block. See this blog post but it uses an if clause if you don't mind.
If you are optimizing your website, maybe consider using gzip on too in your conf. It compress the content before sending them to your clients. It saves you a lot of bandwidth and I think it reduce the latency (faster load).
If you are decided to use gzip with pagespeed, make sure to add below line to your conf and read
pagespeed FetchWithGzip on;
Related
I try to deploy a Flask App on centOS with NGINX. The Flask app is served on 127.0.0.1:5000 and is also accessible with curl 127.0.0.1:5000.
I tried to keep it simple and removed all NGINX config files in conf.d and just use nginx.conf. This is the whole content of the nginx.conf file:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:5000/;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
However, NGINX still shows its default page and there seems to be nothing in the world or at least in the nginx.conf file to change this. Is there anything in NGINX that still might require reconfiguration or anything on centOS that could lead to the problem?
I solved it. It was indeed open NGINX commands.
After I ran the following commands it worked:
sudo systemctl stop nginx
sudo systemctl enable nginx
sudo systemctl restart nginx
Maybe it helps someone in the future.
I have the file on the Vagrant named index.html, and config the nginx a web server vhost like my.loc, the index.html can access in chrome on my Mac, which the vagrant's host.
Now the problem is :
when I edit the index.html file such as
<html>
<head>
<title>title11</title>
</head>
<body>
</body>
</html>
and then I change the title11 to title22, the web page not change on the chrome.
So first I think it's the cache problem, and I config the nginx.conf like this to forbid the cache :
location ~.*\.(js|css|html|png|jpg)$
{
#expires -1;
add_header Cache-Control no-cache;
add_header Cache-Control no-store;
}
it doesn't work. However, I found that if I run the cmd touch /myweb/index.html on the vagrant, the web page changes on Chrome, but it's not what I want.
EDIT:
this is my nginx.conf
user nginx;
worker_processes auto;
error_log /vagrant/nginx-error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /vagrant/nginx-access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /vagrant/nginx-vhosts/*.conf;
}
Now I found if I edit the index.html on linux in the vagrant, and then the page will change on chrome in my Mac OS.So I think the problem is about vagrant.
I finally resolve the problem to change sendfile on to sendfile off in the nginx.conf.
I get the answer from this article : Clear nginx Cache in Vagrant
This is because:
Sendfile is used to ‘copy data between one file descriptor and another‘ and apparently has some real trouble when run in a virtual machine environment, or at least when run through Virtualbox. Turning this config off in nginx causes the static file to be served via a different method and your changes will be reflected immediately and without question
It seems like a simple problem but I cannot find any official information about how to fix it.
The problem: Deploying an EmberJS app to Nginx - all routes return 404's.
The root route / works just fine. However any route besides / fails & returns 404's. Here's how I have my nginx.conf setup:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/ivo/myapp;
index index.html;
location / {
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html?/$request_uri;
}
}
}
This config is largely based on the nginx.conf that comes with the Nginx Docker image, which I am using.
From what I understand about Nginx, the location / block should be handling my routes, but for some reason it is not.
As a side note, I am shocked that EmberJS does not have any documentation about this at all. I even tried their Nginx config from their deployment section and it too did not work - https://ember-cli.com/user-guide/#deployments
Found & "fixed" the problem. The underlying issue is that the information provided on the Nginx DockerHub page is misleading. It says:
"If you wish to adapt the default configuration, use something like the following to copy it from a running nginx container"
And it provides this line as an example:
"COPY nginx.conf /etc/nginx/nginx.conf"
However, the file located at /etc/nginx/nginx.conf was not the file I needed. Instead, the file I needed to change/replace was located here : "/etc/nginx/conf.d/default.conf"
The code I pasted above in the Question portion worked fine; it was the location that was wrong.
In my /etc/nginx/nginx.conf file I have config. as:-
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
Now, I don't like to pollute above default nginx.conf file, so I kept configuration in /etc/nginx/conf.d/default.conf as:-
worker_processes 2;
events {
worker_connections 2048;
}
My question is, In above scenario will nginx override or pick configuration for worker_processes and worker_connections from default.conf file or nginx.conf file ? Also, I would like to know how nginx processes configuration files in short ?
For "how nginx processes configuration files", a simple way to look at it would be:
reading of configuration starts with /etc/nginx/nginx.conf
directives are read from top to bottom
includeing a file inserts it at the location of the include
similar to the way the C preprocessor does
a setting has a scope, such as http, server, or location, in order from wide to narrow
some settings can occur at multiple scope levels
setting a parameter at a narrower scope overrides a setting of the same parameter at a wider scope
conflicting settings within the same scope are rejected
For detail, see the Debian wiki article on Nginx directory structure.
I'm sure that configuration in default.conf will be picked first by nginx, thats why the line "include /etc/nginx/conf.d/*.conf;" is included to override any defaults and add functionality.
I am new to NGINX and I can't tell if I set my conf file up properly. I am trying to serve an index.html file located in the data/www directory. I did a lot of research and I can't seem to figure it out. I have nginx installed on a ec2 running centOS 6.5. Any pointers on how to get this up and running would be greatly appreciated. I have been trying to wrap my head around this for a while now. Thank you in advance!
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name <my elastic ip address>;
location / {
root /data/www/;
index index.html;
}
}
}
Yes,
Your Configuration file is correct.
NOTE:
working:First check your nginx server is running or not, by typing your ip:port(that you given in conf file) in browser.If running it will display nginx current version details.
ACCESS: you can access your file by using command like this.
http://your_ip_addr:port/application name/file_name
*application name:the name given for that app,
for example
location /html {
......
}
then you application name will become html.