"ssl_preread" is not working in NGINX - nginx

I am trying to implement "ssl_preread" in my nginx. My nginx is compiled with "--with-stream_ssl_preread_module" this module.
I mentioned "ssl_preread on;" in server directive of nginx.conf. But i am getting below error.
nginx: [emerg] "ssl_preread" directive is not allowed here in /opt/nginx/conf/nginx.conf:43
I am following below doc.
http://nginx.org/en/docs/stream/ngx_stream_ssl_preread_module.html

Compile with both modules
--with-stream
--with-stream_ssl_preread_module
Create a stream block outside http block
stream {
upstream app {
server IP1:Port;
server IP2:Port;
}
map $ssl_preread_server_name $upstream {
default app;
}
server {
listen PORT;
ssl_preread on;
proxy_pass $upstream;
}
}
This worked for me. Let me know if this works for you too

Maybe U need to update Nginx to lastest version. 1.10 I meet this error too, but 1.18 the config is OK.

Perhaps this is a late answer. I had the following issue when installing nginx 1.14 from CentOS 8.3 repo.
The error was: MisconfigurationError('Error while running nginx -c /etc/nginx/nginx.conf -t.\n\nnginx: [emerg] unknown "ssl_preread_server_name" variable\nnginx: configuration file /etc/nginx/nginx.conf test failed\n',)
The nginx plugin is not working; there may be problems with your existing configuration.
The error was: MisconfigurationError('Error while running nginx -c /etc/nginx/nginx.conf -t.\n\nnginx: [emerg] unknown "ssl_preread_server_name" variable\nnginx: configuration file /etc/nginx/
nginx.conf test failed\n',)
I fixed it by upgrading the very latest nginx-1:1.20.0-1.el8.ngx.x86_64, following this URL
http://nginx.org/en/linux_packages.html#RHEL-CentOS

Related

Running If Statement in NGINX website configuration files

Im trying to use an if statement that allows me to ignore headers only when caching a request. This is the code I implemented ( on /etc/nginx/sites-available/example.com) :
if ($skip_cache = 0) {
fastcgi_ignore_headers X-Accel-Expires Expires Cache-Control;
}
When I run a check on the configuration file's syntax, i get the following error:
nginx: [emerg] "fastcgi_ignore_headers" directive is not allowed here in /etc/nginx/sites-enabled/chartership.com:46
nginx: configuration file /etc/nginx/nginx.conf test failed
Do you guys have any suggestions to steer the implementation correctly?

How to setup nginx for a Flask app on a server with Plesk installed

I want to deploy a little Flask webapp on a root cloud server with Plesk installed. I followed this tutorial as it uses Nginx and Gunicorn, like proposed by the developer of Flask: Miguel Grinberg.
The problem is that Plesk has a modified variant of nginx pre-installed named sw-nginx and I've absolutely no clue on how to implement that line of code:
server {
listen 80;
server_name my.subdomain.com;
location / {
include proxy_params;
proxy_pass http://unix:/var/www/vhosts/mydomain.com/my.subdomain.com/myproject/app.sock;
}
}
If I just put in in a .conf file, the configtest of Nginx fails:
$ nginx -t
nginx: [emerg] open() "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/conf.d/myproject.conf:6
nginx: configuration file /etc/nginx/nginx.conf test failed
Maybe the syntax of the proxy_pass value is wrong (I tried many, here I put one of them), or maybe the proxy_pass param isn't supported or has to be implemented somewhere else?
PS: I also found this answer How to deploy Flask project on Plesk subdomain
But I read somewhere, that replacing sw-nginx by the standard nginx wouldn't be supported by Plesk. Don't want to break anything...
Thanks for your help.

Why can't NumRewriteThreads and NumExpensiveRewriteThreads be used with Nginx PageSpeed?

So I was trying some settings of the Nginx PageSpeed module, but there was two settings that I couldn't set for some reason.
pagespeed NumRewriteThreads 2;
pagespeed NumExpensiveRewriteThreads 2;
For some reason these two settings always throw error when I try to restart or test my nginx config.
sudo service nginx restart
[FAIL] Restarting nginx: nginx failed!
When I test the file I get this:
"pagespeed" directive "NumRewriteThreads" not recognized or too many arguments in /etc/nginx/nginx.conf:40
nginx: configuration file /etc/nginx/nginx.conf test failed
My Nginx version is the following:
sudo /usr/sbin/nginx -v
nginx version: nginx/1.6.0
The settings appear in the PageSpeed docs as well, so I'm wondering if there's something I am missing to make them work.
https://developers.google.com/speed/pagespeed/module/system#tune_thread
I've entered a bug for this: ngx_pagespeed #728. I think it's just an oversight.

getting error while using lua with nginx

I am very new to nginx and lua .i have installed Openresty .
below is my code in nginx.conf file .
server{
location /hellolua {
default_type 'text/plain';
content_by_lua ' local name = ngx.var.arg_name or "Anonymous"
ngx.say("Hello, ", name, "!") ';
}
}
When i am running sudo service nginx start i am getting error Starting nginx: nginx: [emerg] unknown directive "content_by_lua" in /etc/nginx/nginx.conf:24
nginx: configuration file /etc/nginx/nginx.conf test failedt
Please let me know what i am missing .
It seems to me, as if you haven't installed the right module? ngx_lua (http://wiki.nginx.org/HttpLuaModule)
You mention OpenResty. Did you configure it with lua? If not, the guide is here(http://wiki.nginx.org/HttpLuaModule#Installation).
Quick resumé:
The ngx_openresty bundle can be used to install Nginx, ngx_lua, either one of the standard Lua 5.1 interpreter or LuaJIT 2.0, as well as a package of powerful companion Nginx modules. The basic installation step is a simple ./configure --with-luajit && make && make install.
You can manually compile ngx_lua into nginx too, the full guide is in the link too.
After comment-discussing - I removed the irrelevant part of the answer.
By default, OpenResty's nginx is installed into the path /usr/local/openresty/nginx/sbin/nginx. Your system's default nginx init configurations need an update to point to the right locations.

nginx - client_max_body_size has no effect

nginx keeps saying client intended to send too large body. Googling and RTM pointed me to client_max_body_size. I set it to 200m in the nginx.conf as well as in the vhost conf, restarted Nginx a couple of times but I'm still getting the error message.
Did I overlook something? The backend is php-fpm (max_post_size and max_upload_file_size are set accordingly).
Following nginx documentation, you can set client_max_body_size 20m ( or any value you need ) in the following context:
context: http, server, location
NGINX large uploads are successfully working on hosted WordPress sites, finally (as per suggestions from nembleton & rjha94)
I thought it might be helpful for someone, if I added a little clarification to their suggestions. For starters, please be certain you have included your increased upload directive in ALL THREE separate definition blocks (server, location & http). Each should have a separate line entry. The result will like something like this (where the ... reflects other lines in the definition block):
http {
...
client_max_body_size 200M;
}
(in my ISPconfig 3 setup, this block is in the /etc/nginx/nginx.conf file)
server {
...
client_max_body_size 200M;
}
location / {
...
client_max_body_size 200M;
}
(in my ISPconfig 3 setup, these blocks are in the /etc/nginx/conf.d/default.conf file)
Also, make certain that your server's php.ini file is consistent with these NGINX settings. In my case, I changed the setting in php.ini's File_Uploads section to read:
upload_max_filesize = 200M
Note: if you are managing an ISPconfig 3 setup (my setup is on CentOS 6.3, as per The Perfect Server), you will need to manage these entries in several separate files. If your configuration is similar to one in the step-by-step setup, the NGINX conf files you need to modify are located here:
/etc/nginx/nginx.conf
/etc/nginx/conf.d/default.conf
My php.ini file was located here:
/etc/php.ini
I continued to overlook the http {} block in the nginx.conf file. Apparently, overlooking this had the effect of limiting uploading to the 1M default limit. After making the associated changes, you will also want to be sure to restart your NGINX and PHP FastCGI Process Manager (PHP-FPM) services. On the above configuration, I use the following commands:
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart
As of March 2016, I ran into this issue trying to POST json over https (from python requests, not that it matters).
The trick is to put "client_max_body_size 200M;" in at least two places http {} and server {}:
1. the http directory
Typically in /etc/nginx/nginx.conf
2. the server directory in your vhost.
For Debian/Ubuntu users who installed via apt-get (and other distro package managers which install nginx with vhosts by default), thats /etc/nginx/sites-available/mysite.com, for those who do not have vhosts, it's probably your nginx.conf or in the same directory as it.
3. the location / directory in the same place as 2.
You can be more specific than /, but if its not working at all, i'd recommend applying this to / and then once its working be more specific.
Remember - if you have SSL, that will require you to set the above for the SSL server and location too, wherever that may be (ideally the same as 2.). I found that if your client tries to upload on http, and you expect them to get 301'd to https, nginx will actually drop the connection before the redirect due to the file being too large for the http server, so it has to be in both.
Recent comments suggest that there is an issue with this on SSL with newer nginx versions, but i'm on 1.4.6 and everything is good :)
You need to apply following changes:
Update php.ini (Find right ini file from phpinfo();) and increase post_max_size and upload_max_filesize to size you want:
sed -i "s/post_max_size =.*/post_max_size = 200M/g" /etc/php5/fpm/php.ini
sed -i "s/upload_max_filesize =.*/upload_max_filesize = 200M/g" /etc/php5/fpm/php.ini```
Update NginX settings for your website and add client_max_body_size value in your location, http, or server context.
location / {
client_max_body_size 200m;
...
}
Restart NginX and PHP-FPM:
service nginx restart
service php5-fpm restart
NOTE: Sometime (In my case almost every time) you need to kill php-fpm process if it didn't refresh by service command properly. To do that you can get list of processes (ps -elf | grep php-fpm) and kill one by one (kill -9 12345) or use following command to do it for you:
ps -elf | grep php-fpm | grep -v grep | awk '{ print $4 }' | xargs kill -9
Please see if you are setting client_max_body_size directive inside http {} block and not inside location {} block. I have set it inside http{} block and it works
Someone correct me if this is bad, but I like to lock everything down as much as possible, and if you've only got one target for uploads (as it usually the case), then just target your changes to that one file. This works for me on the Ubuntu nginx-extras mainline 1.7+ package:
location = /upload.php {
client_max_body_size 102M;
fastcgi_param PHP_VALUE "upload_max_filesize=102M \n post_max_size=102M";
(...)
}
I had a similar problem recently and found out, that client_max_body_size 0; can solve such an issue. This will set client_max_body_size to no limit. But the best practice is to improve your code, so there is no need to increase this limit.
I meet the same problem, but I found it nothing to do with nginx. I am using nodejs as backend server, use nginx as a reverse proxy, 413 code is triggered by node server. node use koa parse the body. koa limit the urlencoded length.
formLimit: limit of the urlencoded body. If the body ends up being larger than this limit, a 413 error code is returned. Default is 56kb.
set formLimit to bigger can solve this problem.
Assuming you have already set the client_max_body_size and various PHP settings (upload_max_filesize / post_max_size , etc) in the other answers, then restarted or reloaded NGINX and PHP without any result, run this...
nginx -T
This will give you any unresolved errors in your NGINX configs. In my case, I struggled with the 413 error for a whole day before I realized there were some other unresolved SSL errors in the NGINX config (wrong pathing for certs) that needed to be corrected. Once I fixed the unresolved issues I got from 'nginx -T', reloaded NGINX, and EUREKA!! That fixed it.
I'm setting up a dev server to play with that mirrors our outdated live one, I used The Perfect Server - Ubuntu 14.04 (nginx, BIND, MySQL, PHP, Postfix, Dovecot and ISPConfig 3)
After experiencing the same issue, I came across this post and nothing was working. I changed the value in every recommended file (nginx.conf, ispconfig.vhost, /sites-available/default, etc.)
Finally, changing client_max_body_size in my /etc/nginx/sites-available/apps.vhost and restarting nginx is what did the trick. Hopefully it helps someone else.
In case you are using Kubernetes, add the following annotations to your Ingress:
annotations:
nginx.ingress.kubernetes.io/client-max-body-size: "5m"
nginx.ingress.kubernetes.io/client-body-buffer-size: "8k"
nginx.ingress.kubernetes.io/proxy-body-size: "5m"
nginx.ingress.kubernetes.io/proxy-buffer-size: "8k"
Confirm the changes were applied:
kubectl -n <namespace> describe ingress <ingress-name>
References:
Client Body Buffer Size
Custom max body size
Had the same issue that the client_max_body_size directive was ignored.
My silly error was, that I put a file inside /etc/nginx/conf.d which did not end with .conf. Nginx will not load these by default.
If you tried the above options and no success, also you're using IIS (iisnode) to host your node app, putting this code on web.config resolved the problem for me:
Here is the reference: https://www.inflectra.com/support/knowledgebase/kb306.aspx
Also, you can chagne the length allowed because now I think its 2GB. Modify it by your needs.
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
The following config worked for me. Notice I only set client_max_body_size 50M; once, contrary to what others are saying...
File: /etc/nginx/conf.d/sites.conf
server {
listen 80 default_server;
server_name portal.myserver.com;
return 301 https://$host$request_uri;
}
server {
resolver 127.0.0.11 valid=30s;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /secret/portal.myserver.com.crt;
ssl_certificate_key /secret/portal.myserver.com.pem;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server_name portal.myserver.com;
client_max_body_size 50M;
location /fileserver/ {
set $upstream http://fileserver:6976;
proxy_pass $upstream;
}
}
If you are using windows version nginx, you can try to kill all nginx process and restart it to see.
I encountered same issue In my environment, but resolved it with this solution.

Resources