I have a development server that is running Nginx and serving a PHP website.
Below is the working nginx config.
cat /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
client_max_body_size 100M;
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;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
This works perfectly to serve the PHP site; however, I would also like Nginx to run a Node based websockets server.
What changes do I need to make to this Nginx config to receive websockets traffic as well?
I created a new file at:
vi /etc/nginx/sites-enabled/somesite.com
and put this in it:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server localhost:3000;
}
server {
listen 8080;
server_name somesite.com;
access_log /var/log/nginx/websocket.access.log;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
This seems to work.
Related
I have installed nginx on ubuntu 20.04 server. Simple HTML sites are working fine. Later i decided to install php-fpm. I have phpinfo.php file in my /var/www/html folder with following code
<?php
phpinfo ();
?>
Now when i point to this file in my browser my browser downloads phpinfo.php file instead of showing me my PHP information page. I believe my PHP is not working or my nginx is not using or passing my request through PHP. I also believe that there is something i'm missing in default file sitting in /etc/nginx/sites-available folder. This missing part is suppose to use installed php on this server. My nginx.conf file is:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
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;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
My default file in /etc/nginx/sites-available is
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php index.html index.htm;
server_name server_domain_name_or_IP;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
What is the situation?
2 websites: A and B ( laravel5.2 )
A want to using B's javascript file
B is using opcache
B using git update codes and post-hook request specified URL reset opcache
website A codes:
<script src="https://B/controller/getJsFunction"></script>
website B codes:
public function getJsFunction()
{
header('Content-Type:application/javascript');
header('Cache-Control: max-age=0, must-revalidate');
//mix('/js/visitor.js') will get visitor.js and auto add the current version
echo file_get_contents(rtrim(public_path(), '/') . mix('/js/visitor.js'));
}
What I want?
website A get the website B's javascript file content
B can upgrade the javascript file version number any time, visitor.v1.js => visitor.v2.js
What happened?
B sustain return 502 HTTP status code after a period of successful operation,
When B Modified any code trigger opcache_reset(), even just add comments, the bug will fix a while.
What I have?
nginx log
2018/05/06 03:56:17 [error] 12747#0: *28155344 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: xxxxxxx, server: xxxxxxx.com, request: "GET /stateless/visitor/js HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xxxxxx.com", referrer: "https://xxxxxx"
git post-receive
cd /path/to/site-B/project
unset GIT_DIR
git pull origin master && curl https://site-B.com/cache.php
cache.php
<?php
opcache_reset();
nginx.conf
user nginx nginx;
worker_processes auto;
error_log /var/log/nginx/error.log error;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include /etc/nginx/black.ip;
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"';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 600;
fastcgi_read_timeout 600;
types_hash_max_size 2048;
client_max_body_size 20m;
gzip on;
index index.html index.htm index.php;
server {
listen 80 default;
server_name _;
return 403;
}
server {
listen 80;
listen 443;
server_name site-b.com;
root /path/to/site-b.com/public;
ssl on;
ssl_certificate /path/to/site-b.com.crt;
ssl_certificate_key /path/to/site-b.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
if ($scheme = http ) {
rewrite ^(.*)$ https://$host$1 permanent;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.(jpg|jpeg|png|gif|js|css|ico|eot|svg|ttf|woff|woff2)$
{
expires max;
add_header Cache-Control public;
add_header Pragma public;
add_header Vary Accept-Encoding;
}
location ~* \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
What help I need?
How to REALLY fix the bug?
Or what other methods should I use?
Or why reset_opcache() can fix the bug? Or why opcache causes this bug?
Since this is an online project, I temporarily modified it to that site A first request site B to get the current version of the javascript file, and then insert a script tag and set src url from first step response
I have a problem with my nginx configuration. We have a Vagrant box at the firm. In this vagrant we have LXC containers for services like nginx container, php-fpm container, memcached container, mysql container... These are connecting to each other, nginx use php-fpm, php-fpm use memcached and mysql. I have access to the nginx outside the vagrant through https. Here is my nginx configuration:
nginx.conf:
user nginx nginx;
worker_processes 4;
pid /var/run/nginx.pid;
worker_rlimit_nofile 1024;
events {
worker_connections 2048;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
access_log "/var/log/nginx/access.log";
error_log "/var/log/nginx/error.log";
keepalive_timeout 120;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
deault.conf:
server {
listen *:80;
server_name vagrant.ceg.com;
return 301 'https://$server_name$request_uri';
}
https.conf:
server {
listen *:443;
ssl on;
ssl_certificate ....crt;
ssl_certificate_key ....key;
server_name vagrant.ceg.com www.vagrant.ceg.com;
root "/srv/www";
index index.php;
location / {
autoindex on;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_param ENVIRONMENT dev;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
if (-f $request_filename) {
fastcgi_pass 192.168.42.114:9000;
}
}
}
When I open it in the browser I get the index.php, but very slowly, and I got same errors on the console like this:
https://www.vagrant.ceg.com/cdn/util/scale/320/320/dev-employer-images/0636443e3076af9d24ba2b1711f57fb47b60f289.jpg Failed to load resource: net::ERR_CONNECTION_TIMED_OUT
I am using WT-NMP software with combination of php,mysql and ngnix server.
worker_processes 1;
events {
worker_connections 1024;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
ssi off;
#Timeouts
client_body_timeout 5;
client_header_timeout 5;
keepalive_timeout 25 25;
send_timeout 15s;
resolver_timeout 3s;
#Directive sets timeout period for connection with FastCGI-server. It should be noted that this value can't exceed 75 seconds.
fastcgi_connect_timeout 5s;
#Directive sets the amount of time for upstream to wait for a fastcgi process to send data. Change this directive if you have long running fastcgi processes that do not produce output until they have finished processing. If you are seeing an upstream timed out error in the error log, then increase this parameter to something more appropriate.
fastcgi_read_timeout 40s;
#Directive specifies request timeout to the server. The timeout is calculated between two write operations, not for the whole request. If no data have been written during this period then serve closes the connection.
fastcgi_send_timeout 15s;
fastcgi_buffers 8 32k;
fastcgi_buffer_size 32k;
#fastcgi_busy_buffers_size 256k;
#fastcgi_temp_file_write_size 256k;
open_file_cache off;
#php max upload limit cannot be larger than this
client_max_body_size 8m;
####client_body_buffer_size 1K;
client_header_buffer_size 1k;
large_client_header_buffers 2 1k;
types_hash_max_size 2048;
include nginx.mimetypes.conf;
default_type text/html;
##
# Logging Settings
##
access_log "c:/wt-nmp/log/nginx_access.log";
error_log "c:/wt-nmp/log/nginx_error.log" warn; #debug or warn
log_not_found on; #enables or disables messages in error_log about files not found on disk.
rewrite_log off;
#Leave this off
fastcgi_intercept_errors off;
gzip off;
index index.php index.htm index.html;
server {
listen 127.0.0.1:80 default_server;
listen 127.0.0.1:8080;
#listen [::1]:80 ipv6only=on;
server_name mylocalhost;
root "c:/wt-nmp/www/projectname";
autoindex on;
error_log "c:/wt-nmp/log/nginx_error.log";
allow 127.0.0.1;
#allow ::1;
deny all;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
#tools are now served from wt-nmp/include/tools/
location ~ ^/tools/.*\.php$ {
root "c:/wt-nmp/include";
try_files $uri =404;
include nginx.fastcgi.conf;
fastcgi_pass php_farm;
}
location ~ ^/tools/ {
root "c:/wt-nmp/include";
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_farm;
include nginx.fastcgi.conf;
}
}
include domains.d/*.conf;
include nginx.phpfarm.conf;
}
when I am trying to access with "mylocalhost" its working fine when I am firing an event and call ajax method . It is giving page not found message
WT-NMP - portable Nginx Mysql Php development stack for Windows README.md states:
Starting only one PHP-CGI server with wt-nmp.exe --phpCgiServers=1 will result in slow ajax requests since Nginx will not be able to process PHP scripts simultaneous.
So, make sure you use the latest version of WT-NMP and choose at least 3 PHP-CGI servers.
I am trying to setup nginx as a caching reverse proxy, however it would appear that every request is been sent to the backend server, and nothing is been cached. i.e. the server logs on the backend show all the same file accesses.
Most of the files are either php with arguments passed on the url or images, all of which are been fetched all the time from the backend and never cached. Everything on this site can be cached.
My conf.d/default.conf
upstream xxxx {
server xxxx.com;
}
#
# The default server
#
server {
listen 80 default_server;
server_name _;
access_log /var/log/nginx/log/access.log main;
error_log /var/log/nginx/log/error.log;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
## send request back to xxxx ##
proxy_pass http://xxxx;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
# expires 24h;
# add_header Cache-Control public;
proxy_ignore_headers Cache-Control Expires;
proxy_redirect off;
proxy_buffering off;
proxy_cache one;
proxy_cache_key backend$request_uri;
proxy_cache_valid 200 301 302 1440m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1440m;
proxy_cache_use_stale error timeout invalid_header updating;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
and my nginx.conf file
user nginx;
worker_processes 8;
worker_rlimit_nofile 8192;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
worker_connections 2048;
}
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;
server_names_hash_bucket_size 64;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_comp_level 9;
gzip_proxied any;
proxy_buffering on;
proxy_cache_path /usr/local/nginx/proxy levels=1:2 keys_zone=one:1024m inactive=7d max_size=700g;
proxy_temp_path /tmp/nginx/proxy;
proxy_buffer_size 4k;
proxy_buffers 100 8k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
include /etc/nginx/conf.d/*.conf;
}
Can anybody tell me what I've got wrong??
I ran into this problem as well, and I found
proxy_buffering off;
Will cause nginx to bypass cache and not save the file to disk.
Remove that line and then it works.
Your upstream server responses must be settings Cookies, please see
https://stackoverflow.com/a/10995522/482926